Skip to content

Tag: postgresql

How to get Postgres to return 0 for empty rows

I have a query which get data summarised between two dates like so: However for dates that do not have any orders, the query returns nothing and I’d like to return 0. I have tried with COALESCE but that doesn’t seem to do the trick? Any suggestions? Answer Please refer to the below script. There i…

How to leave only distinct rows in the table. Postgresql

I need to delete all duplicate in 40kk of rows. I’ve got table: I’ve tried this query but after 1h of executing I’ve gave up on waiting. Is there any other solution to delete duplicates in more optimazed way? UPD: I need to do it just once cause I didn’t handle the duplicates at the st…

fill in missing dates of date_trunc with last row data

I currently have a query for chart using date_trunc and the result is something like this dates with no value are skipped , however I need it to be like this notice how for 7th and 8th it will use the last available data which is in 6th. Will appreciate any help! Answer Use generate_series to generate a row f…

Postgres Query Using Join Table

I have 3 tables: users, public_pictures, pictures Import properties: users.id public_pictures.user_id public_pictures.picture_id pictures.id public_pictures is a join table between users and pictures with only those two properties above. Both are foreign keys to their respective table. Given a user.id, I want…