我有一个工作查询,不知道它是否有效.
表列:
要执行的操作:
注意:我也在检查每个选择中的id和fid虽然我需要检查所有选择的相同值.
现有查询:
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)
Run Code Online (Sandbox Code Playgroud)
够好吗?如果有人可以建议一种方法,使其更有效,那将是伟大的.
你可以这样做
select
sum(Case when a.p_type = 'sell' then a.unit else null end) as sellUnit,
sum(Case when a.p_type = 'CancelSell' then a.unit else null end) as CancelSellUnit,
sum(Case when a.p_type = 'Bank' then a.unit else null end) as BankUnit ,
sum(Case when a.p_type = 'CancelBank' then a.unit else null end) as CancelBankUnit
from table1 a where and a.id=1 and a.fid=2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
96 次 |
| 最近记录: |