0

我有一个看起来像这样的结果集。我想合计支付的金额并将 DTV TOW 和 FlatBED 合并为 1 行。有什么办法可以完成吗?

CaseServiceID   PurchaseOrderID PaidAmount  DTV TOW FLATBED
227                 15000227    19.20           1   0
227                 15000227    45.00           0   1

注意:DTV TOWFLATBED列来自透视表

编辑

枢轴看起来像这样:

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
4

3 回答 3

2

怎么样

SELECT CaseServiceID, 
       PurchaseOrderID, 
       SUM(PaidAmount) PaidAmount,
       SUM([DTV TOW]) [DTV TOW],
       SUM(FLATBED) FLATBED
FROM YourTable
GROUP BY CaseServiceID, PurchaseOrderID
于 2013-09-12T15:53:14.160 回答
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)
于 2013-09-12T18:17:24.333 回答
-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
于 2013-09-13T19:17:17.360 回答