I have an working query and not sure if its efficient or not.
Table columns:
- p_type: can be STRING sell, cancelsell, bank, cancelbank
- id
- fid
- unit
Operation to be performed:
- SUM(unit) for each process type sell,cancelsell,bank,cancelbank.
- SUM(UNIT for sell) - SUM(unit for cancelsell) , SUM(unit for bank) - SUM(unit for cancelbank)
NOTE: I am also checking for id and fid in each select though I need to check with same values for all select.
The existing query:
select sell-cancelsell scount, bank-cancelbank bcount from
(select sum(a.unit) as sell from table1 a where
a.p_type = 'Sell' and a.id=1 and a.fid=2 ),
(select sum(c.unit) as cancelsell from table1 c where
c.p_type = 'CancelSell' and c.id=1 and c.fid=2),
(select sum(b.unit) as bank from table1 b where
b.p_type = 'Bank' and b.id=1 and b.fid=2),
(select sum(d.unit) as cancelbank from table1 d where
d.p_type = 'CancelBank' and d.id=1 and d.fid=2)
Is good enough? If anyone can suggest a way to make it more efficient that would be great.