Say I have a table like the following:
id quantity
1 5
2 3
3 6
I want to retrieve the quantity of the row where id=2, and the total quantity of the table, I thought I could do something like the following:
sql="select quantity where id='2' sum(quantity) as total_quantity from table";
now, I know that this statement is wrong, but I hope you can get the idea of what I am trying to do here. How can I achieve something like this?
Use conditional aggregation:
select sum(case when id = 2 then quantity end) as qty2,
sum(quantity) as total_quantity
from t;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With