如何使用其他列值汇总列值

时间:2016-06-02 14:18:52

标签: sql-server

我有一个存储过程

ALTER  PROCEDURE [dbo].[PaymentServiceDetailBusinessUnit]
(
--@MRRId INT ,
@POId INT ,
@SDId INT 

)

AS
BEGIN

SELECT  BusinessUnit  , E1Description,Fund,SpendPriority,ServiceType,
 REUId , RCUId,  POId, PRR_Id, SUM (PaymentAmount) as TotalPaymentAmount
 FROM 
 (
SELECT  BU.BusinessUnit , BU.E1Description,BU.Fund,BU.SpendPriority,BU.ServiceType,
REU.Id AS REUId ,RCU.Id AS RCUId, PaymentAmount,PO.Id as POId, REU.PRR_Id
FROM [EBSTest].[dbo].[PaymentDetail] PD
     JOIN PurchaseOrder PO ON PO.Id = PD.PO_Id
     JOIN fBusinessUnit BU ON BU.id = PD.BU_Id
LEFT JOIN Reimbursement_EBSUtilization REU ON REU.Id = PD.REU_Id
LEFT JOIN Reimbursement_CDSUtilization RCU ON RCU.Id = PD.RCU_Id
LEFT JOIN ProviderReimbursementRequest PRR ON (PRR.Id = REU.PRR_Id  OR PRR.Id = RCU.PRR_Id)
LEFT JOIN CDSUtilization CDS ON CDS.Id = RCU.CDSU_Id


WHERE 
PO.Id = @POId  AND 
--PRR.MRR_Id = @MRRId AND 
(REU.SD_Id = @SDId OR CDS.ServiceDetail_Id  = @SDId)
) AS dt

GROUP BY 
BusinessUnit , E1Description,Fund,SpendPriority,ServiceType,REUId , RCUId, POId,PRR_Id



END 

输出是

BusinessUnit    E1Description   Fund    SpendPriority   ServiceType REUId RCUId POId    PRR_Id  TotalPaymentAmount
10000002    MVBsd1hnhg  CF  Normal  SA  2331    NULL    12  934 100.00
10000002    MVBsd1hnhg  CF  Normal  SA  2346    NULL    12  935 60.00
10000002    MVBsd1hnhg  CF  Normal  SA  2361    NULL    12  937 40.00
10000002    MVBsd1hnhg  CF  Normal  SA  2389    NULL    12  941 40.00
10000002    MVBsd1hnhg  CF  Normal  SA  2406    NULL    12  943 40.00

但我想只显示一条记录,其中包含总付款金额的总和 使用businessunit列-10000002 即

像这样的事情

**10000002**    MVBsd1hnhg  CF  Normal  SA  2331    NULL    12  934 **280.00**

任何人都可以帮我吗?

2 个答案:

答案 0 :(得分:2)

REUId , RCUId, POId,PRR_Id子句中删除Group By,因为这些似乎导致了总和聚合的分区。

SELECT  BusinessUnit  , E1Description,Fund,SpendPriority,ServiceType,
 SUM (PaymentAmount) as TotalPaymentAmount
 FROM 
 (
SELECT  BU.BusinessUnit , BU.E1Description,BU.Fund,BU.SpendPriority,BU.ServiceType,
REU.Id AS REUId ,RCU.Id AS RCUId, PaymentAmount,PO.Id as POId, REU.PRR_Id
FROM [EBSTest].[dbo].[PaymentDetail] PD
     JOIN PurchaseOrder PO ON PO.Id = PD.PO_Id
     JOIN fBusinessUnit BU ON BU.id = PD.BU_Id
LEFT JOIN Reimbursement_EBSUtilization REU ON REU.Id = PD.REU_Id
LEFT JOIN Reimbursement_CDSUtilization RCU ON RCU.Id = PD.RCU_Id
LEFT JOIN ProviderReimbursementRequest PRR ON (PRR.Id = REU.PRR_Id  OR PRR.Id = RCU.PRR_Id)
LEFT JOIN CDSUtilization CDS ON CDS.Id = RCU.CDSU_Id


WHERE 
PO.Id = @POId  AND 
--PRR.MRR_Id = @MRRId AND 
(REU.SD_Id = @SDId OR CDS.ServiceDetail_Id  = @SDId)
) AS dt

GROUP BY 
BusinessUnit , E1Description,Fund,SpendPriority,ServiceType

答案 1 :(得分:0)

类似

;WITH CTE AS
(
        SELECT  BusinessUnit  , E1Description,Fund,SpendPriority,ServiceType,
         REUId , RCUId,  POId, PRR_Id, SUM (PaymentAmount) as TotalPaymentAmount
         FROM 
         (
        SELECT  BU.BusinessUnit , BU.E1Description,BU.Fund,BU.SpendPriority,BU.ServiceType,
        REU.Id AS REUId ,RCU.Id AS RCUId, PaymentAmount,PO.Id as POId, REU.PRR_Id
        FROM [EBSTest].[dbo].[PaymentDetail] PD
             JOIN PurchaseOrder PO ON PO.Id = PD.PO_Id
             JOIN fBusinessUnit BU ON BU.id = PD.BU_Id
        LEFT JOIN Reimbursement_EBSUtilization REU ON REU.Id = PD.REU_Id
        LEFT JOIN Reimbursement_CDSUtilization RCU ON RCU.Id = PD.RCU_Id
        LEFT JOIN ProviderReimbursementRequest PRR ON (PRR.Id = REU.PRR_Id  OR PRR.Id = RCU.PRR_Id)
        LEFT JOIN CDSUtilization CDS ON CDS.Id = RCU.CDSU_Id


        WHERE 
        PO.Id = @POId  AND 
        --PRR.MRR_Id = @MRRId AND 
        (REU.SD_Id = @SDId OR CDS.ServiceDetail_Id  = @SDId)
        ) AS dt

        GROUP BY 
        BusinessUnit , E1Description,Fund,SpendPriority,ServiceType,REUId , RCUId, POId,PRR_Id
),
CTE2 AS 
(
SELECT row_number() OVER(ORDER BY TotalPaymentAmount DESC) AS rown,* FROM CTE  WHERE rown = 1
)
SELECT *, (SELECT SUM(ST.TotalPaymentAmount) FROM CTE2 ST) As TheTotalAmount  FROM CTE2 WHERE CTE2.rown=1;