我目前在 SQL Server 2012 中有以下查询。为除“cd.billed_amt as [Disb Billed]”之外的字段返回单行,单个 bill_num 可以有多个结果。
我想做的只是总结 [Disb Billed] 列,这样最初的 8 行结果集将变成 2 行结果集。
select distinct bbl.bill_num,
bb.tran_type,
hm.clnt_matt_code,
bb.tran_date,
bb.period,
cd.billed_amt as [Disb Billed],
fees_amt
from blt_bill_amt bb
join hbm_matter hm on bb.matter_uno = hm.matter_uno
join blt_billm bbm on bbm.billm_uno = bb.billm_uno
join blt_bill bbl on bbl.tran_uno = bbm.bill_tran_uno
left outer join cdt_disb cd on cd.bill_tran_uno = bbl.tran_uno
where bb.tran_type in ('WO', 'WOX')
and bb.period = '201401'
and bbl.bill_num = 231728
order by bb.tran_type, bbl.bill_num
当前结果集
bill_num tran_type clnt_matt_code tran_date period Disb Billed fees_amt
------------------------------------------------------------------------------------------------
231728 WO N10118.1016 2013-04-18 00:00:00.000 201401 3.00 8.06
231728 WO N10118.1016 2013-04-18 00:00:00.000 201401 20.00 8.06
231728 WO N10118.1016 2013-04-18 00:00:00.000 201401 38.00 8.06
231728 WO N10118.1016 2013-04-18 00:00:00.000 201401 42.50 8.06
231728 WO N10118.1016-0001 2013-04-18 00:00:00.000 201401 3.00 0.94
231728 WO N10118.1016-0001 2013-04-18 00:00:00.000 201401 20.00 0.94
231728 WO N10118.1016-0001 2013-04-18 00:00:00.000 201401 38.00 0.94
231728 WO N10118.1016-0001 2013-04-18 00:00:00.000 201401 42.50 0.94
期望的结果集
bill_num tran_type clnt_matt_code tran_date period Disb Billed fees_amt
------------------------------------------------------------------------------------------------
231728 WO N10118.1016 2013-04-18 00:00:00.000 201401 103.50 8.06
231728 WO N10118.1016-0001 2013-04-18 00:00:00.000 201401 103.50 0.94