需要根据同一表中的特定条件从同一列中进行选择
当status =‘OP’
然后tot_amount
否则0时,我尝试使用最大大小写
当status =‘PR’
和curr_flag =‘Y’
然后tot_amount
否则为0
下面是表格
ID Type Status tot_Amount curr_flag
1 Null OP 100 N
1. F. PR 60 N
1. H. PR. 0. Y
预期产量
ID Type. TotalAmt Bal-Amt
1. H. 100. 0
答案 0 :(得分:1)
您需要条件聚合:
select id,
max(case when curr_falg = 'Y' then Type end) as Type,
sum(case when Status = 'OP' then tot_Amount else 0 end) as TotalAmt,
sum(case when Status = 'PR' and curr_falg = 'Y' then tot_Amount else 0 end) as Bal_Amt
from table t
group by id;