Tsql 合并2行

Tsql 合并2行,tsql,Tsql,我有一个这样的结果集。我想合计支付的金额,并将DTV拖车和平板组合成一排。我有什么办法可以完成吗 CaseServiceID PurchaseOrderID PaidAmount DTV TOW FLATBED 227 15000227 19.20 1 0 227 15000227 45.00 0 1 注:DTV-TOW和flattbed列来自数据透视表 编辑 Pi

我有一个这样的结果集。我想合计支付的金额,并将DTV拖车和平板组合成一排。我有什么办法可以完成吗

CaseServiceID   PurchaseOrderID PaidAmount  DTV TOW FLATBED
227                 15000227    19.20           1   0
227                 15000227    45.00           0   1
注:
DTV-TOW
flattbed
列来自数据透视表

编辑

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
怎么样

SELECT CaseServiceID, 
       PurchaseOrderID, 
       SUM(PaidAmount) PaidAmount,
       SUM([DTV TOW]) [DTV TOW],
       SUM(FLATBED) FLATBED
FROM YourTable
GROUP BY CaseServiceID, PurchaseOrderID
怎么样

SELECT CaseServiceID, 
       PurchaseOrderID, 
       SUM(PaidAmount) PaidAmount,
       SUM([DTV TOW]) [DTV TOW],
       SUM(FLATBED) FLATBED
FROM YourTable
GROUP BY CaseServiceID, PurchaseOrderID
尝试此操作(假设PaidAmount和ClaimQuantity为bit 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)
尝试此操作(假设PaidAmount和ClaimQuantity为bit 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)

查询最终是什么样子的。我希望这对某人有帮助。谢谢

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

查询最终是什么样子的。我希望这对某人有帮助。谢谢

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

你能显示你的pivot查询吗?@DanAndrews,已经更新了。你能显示你的pivot查询吗?@DanAndrews,它已经更新了。ThxFYI,@AndriyM感谢您提供的信息,我总是使用TinyInts而不是bit(用于索引目的),因此其他人使用它们时我会错过问题。仅供参考,@AndriyM感谢您提供的信息,我总是使用TinyInts而不是bit(用于索引目的),因此其他人使用它们时我会错过问题。