I need help with an aggregation functionality.
what I want to know is if it is possible to extract a concrete value from a grouped query on one of the columns I return, like this
STORE
fruit | color | stock |
---|---|---|
apple | red | 30 |
apple | green | 5 |
banana | yellow | 40 |
berry | red | 5 |
pear | green | 5 |
SELECT SUM(stock), [?] FROM store GROUP BY fruit
[?] -> i need to take a concrete value, for example RED. but the SUM must have 35 in apples.
can this be done without a subquery?
Thanks
I expect this results
Column A | Column B |
---|---|
35 | red |
in this case the query does not make sense but for my personal case it does. I try to use STRING_AGG to take the data and make explode in my code, but its not the best way i think
I think you're looking for the GROUP BY clause. Try this:
This will return a list of all colors, and the sum of the stock for each color.