Skip to content
Advertisement

Tag: postgresql

Checking for different columns OR empty result PostgreSQL

I’m writing some query tests for dbt and I’d like to check that two queries return the same thing and that neither is empty. Since it’s a dbt test, I need to return a row if either of these conditions is false. So far I’ve come up with the following: Theoretically, this should return a row with 1 if either

Output array type column

I have a table like so – How do I output this result – Answer For an array, you would use array_agg(): If you want the value as a comma-sseparated string:

How to iteratively use INSERT INTO in PostgreSQL

I have Table1 with rows I want to iterate over, and insert values from those rows of data into another table. So basically pseudo is: Just looping through Table1, adding in one of its values every time. Answer I think you just want an INSERT INTO … SELECT here: Most SQL operations are inherently set based, meaning if you want

Return false if no rows for select

I have next query: It works fine, if we have some records in new_date table, but if it is empty for some user, query just returns empty string. How I can return false, if there are no records also? Answer You could just selects an EXISTS expression here: This would always return just a single record, containing a boolean value

JOIN is MUCH slower than a UNION, even with indexes

I have a query: All tables have indexes. Why is this query taking 4.5s across 6000 rows? Explain analyze output below: I have a UNION query that does the same thing, but is much quicker (0.001s). I want to know more about why my JOIN query is so much slower: Answer OR is always difficult for the optimiser(s). Since you

Advertisement