Basically, I have a table that looks like this:
Fruit | Juiciness | Brand | Price
---------------------------------
Apple | 2 | A | 0.50
Pear | 1 | A | 0.70
Plum | 6 | A | 0.40
Kiwi | 5 | B | 0.80
Grape | 4 | B | 0.01
I'm looking to output two rows, one for brand A and one for brand B, with the name of the juiciest fruit (max juiciness value), and the sum price of all fruits from that brand. So:
Fruit | Brand | Price
---------------------------------
Plum | A | 1.60
Kiwi | B | 0.81
I was thinking of breaking it up into a few different CTE/subqueries, one with a simple SUM(Price)
and one with a DISTINCT ON
and ORDER BY
to get the juiciest fruit and then joining them back together. But somehow even that wasn't working for me. (Something about me not wanting to display the juiciness seems to have been tripping Postgres up, but I'm not sure.)