Distinct ON in an aggregate function in postgres
Implement DISTINCT ON in a subquery or Common Table Expression (CTE):
This WITH statement grabs only distinct "column_A" values in "your_table". The SUM(column_C) will then add up all the distinct entries.
Dealing with multiple rows (AKA herding cats)
Ever tried to take a selfie with more than one cat? Same problem when dealing with multiple rows. We need to make every row sit nice and unique, but still group together. For such "herding cats" tasks, use LATERAL subqueries. Here's how you'd do it:
The LATERAL keyword gives you the ability to make subqueries reference the main query, a tool as helpful as catnip in a selfie session!
One marble per bucket (Visualisation not for cats)
Let's visualize columns as buckets and rows as marbles:
Buckets (⚙️): [Brand, Year, Model] Marbles (🔵): [Different Car Instances]
We're after only ONE marble per Brand (the Newest Model). We can use DISTINCT ON with MAX(Model):
Outcome:
🏆 Brand Bucket: [...Newest Model per Brand]
The DISTINCT ON clause nabs the first row for each group based off the specified ordering (MAX(Model) in our case). It's similar to picking the prettiest marble from each bucket. (And no, cat, you can't play with the marbles).
Refined JSON responses (Hold the cat hair)
Some cats like their dishes served in a particular way (just ask Garfield!). json_build_object lets you custom-create JSON objects with a fine touch:
This serves up a custom JSON object that would make even a finicky cat purr. Remember: COALESCE keeps things neat and tidy by swapping null arrays with empty arrays.
Troubleshooting like a boss-cat: Avoid common hairballs
- Avoid duplicates: Ensure all distinct fields are in your
GROUP BYclause. - Code grooming: Use CTEs for better readability.
- Performance tuning: Balance complexity with indexing and execution plan considerations.
- Null avoidance: Treat nulls with
COALESCEto handle nulls in JSON arrays.
Was this article helpful?