I have the following data:
ID | Field A | Quantity
1 | A | 10
2 | B | 20
3 | C | 30
I would like to sum the fields with ids 1 and 3 in a way that result will be:
ID | Field A | Quantity
1 | A | 40
2 | B | 20
Sounds to me more like code manipulation rather than SQL, but still want to try it.
My DMBS is sql-server.
you can try by using case when
select case when id in(1,3) then 'A' else 'B' end as field,sum(Quantity) as Quantity
from tablename group by case when id in(1,3) then 'A' else 'B' end
I think simple aggregation does what you want:
select min(id) as id, min(fieldA) as fieldA, sum(quantity) as quantity
from t
group by (case when id in (1, 3) then 1 else id end);
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