我有一个看起来像这样的结果集。我想将付费金额相加,并将DTV TOW和FlatBED合并为一行。我有什么方法可以完成吗?
CaseServiceID PurchaseOrderID PaidAmount DTV TOW FLATBED
227 15000227 19.20 1 0
227 15000227 45.00 0 1
注意:DTV TOW
和FLATBED
列来自透视表
修改的
Pivot看起来像这样:
select
*
from
(
select
cla.CaseServiceID
,cla.ServiceTypeProgKey
,cla.ClaimAmount
,cla.PaidAmount
,cla.ClaimQuantity
from
Claim cla
) as srcServiceType
pivot
(
max(ServiceTypeProgKey) for ServiceTypeProgKey in ([FLATBED],[DTV TOW]
) as pvtServiceType
答案 0 :(得分:2)
怎么样
SELECT CaseServiceID,
PurchaseOrderID,
SUM(PaidAmount) PaidAmount,
SUM([DTV TOW]) [DTV TOW],
SUM(FLATBED) FLATBED
FROM YourTable
GROUP BY CaseServiceID, PurchaseOrderID
答案 1 :(得分:2)
试试这个(假设PaidAmount和ClaimQuantity是位 TinyInt用作布尔值)否则将MAX更改为SUM:
SELECT *
FROM (
SELECT cla.CaseServiceID,
cla.ServiceTypeProgKey,
SUM(cla.ClaimAmount) ClaimAmount,
MAX(cla.PaidAmount) PaidAmount,
MAX(cla.ClaimQuantity) ClaimQuantity
FROM Claim cla
GROUP BY cla.CaseServiceID,
cla.ServiceTypeProgKey
) AS srcServiceType
pivot(max(ServiceTypeProgKey) FOR ServiceTypeProgKey IN (
[FLATBED],
[DTV TOW]
) AS pvtServiceType)
答案 2 :(得分:-1)
查询的最终结果如何。我希望这可以帮助别人。感谢
select
,sum(isnull([FLATBED], 0)) AS [FLATBED]
,sum(isnull([DTV TOW], 0)) AS [DTV TOW]
from
(
select
cla.ServiceTypeProgKey,
,sum(cla.PaidAmount) as 'PaidAmount'
,sum(cla.ClaimQuantity) as 'ClaimQuantity'
,sum(cla.PaidQuantity) as 'PaidQuantity'
from
Claim cla
inner join CaseService cse on cla.CaseServiceID = cse.CaseServiceID
group by cse.PurchaseOrderID
) as srcServiceType
pivot
(
count(ServiceTypeProgKey) FOR ServiceTypeProgKey IN ([FLATBED],[DTV TOW])
) as pvtServiceType
...和结果
CaseServiceID PurchaseOrderID PaidAmount DTV TOW FLATBED
227 15000227 64.20 1 1