在sql server中汇总列值

时间:2015-01-28 14:20:01

标签: sql sql-server

在下面的查询中,我想总结freightChargeLoadingandUnloadingcharges的列,并添加新的collumn totalcost值,我尝试了以下代码,无法进行做到这一点。

SELECT DISTINCT GRN.GoodsReceivedNoteNo 
     , CASE WHEN Count(GRND.GoodsReceivedNoteID) OVER(partition BY GRND.GoodsReceivedNoteID) > 1 THEN Isnull(( GRND.FreightCharges ), 0.00)  
            ELSE Isnull(( OEC.FreightCharges ), 0.00) END FreightCharge
     , CASE WHEN Count(GRND.GoodsReceivedNoteID) OVER(partition BY GRND.GoodsReceivedNoteID) > 1 THEN Isnull(( GRND.LoadingCost + GRND.UnloadingCost ), 0.00) 
            ELSE Isnull(( OEC.LoadingCost + OEC.UnloadingCost ), 0.00) END LoadingandUnloadingcharges 
FROM GoodsReceivedNoteDetail GRND  
LEFT OUTER JOIN GoodsReceivedNote GRN ON GRN.GoodsReceivedNoteID=GRND.GoodsReceivedNoteID  
LEFT OUTER JOIN OtherExpenseCost OEC ON OEC.GoodsReceivedNoteID=GRN.GoodsReceivedNoteID   

2 个答案:

答案 0 :(得分:0)

假设你想要FreightCharge和LoadingandUnloadingcharges的总和:

Select SUM(FreightCharge) FreightCharge, SUM(LoadingandUnloadingcharges) LoadingandUnloadingcharges, SUM(FreightCharge) + SUM(LoadingandUnloadingcharges) TotalCost
from
(SELECT DISTINCT GRN.GoodsReceivedNoteNo ,CASE  
WHEN Count(GRND.GoodsReceivedNoteID) OVER(partition BY GRND.GoodsReceivedNoteID) > 1   
THEN Isnull(( GRND.FreightCharges ), 0.00)  
ELSE Isnull(( OEC.FreightCharges ), 0.00)  
END FreightCharge,CASE  
WHEN Count(GRND.GoodsReceivedNoteID) OVER(partition BY GRND.GoodsReceivedNoteID) > 1   
THEN Isnull(( GRND.LoadingCost + GRND.UnloadingCost ), 0.00)  
ELSE Isnull(( OEC.LoadingCost + OEC.UnloadingCost ), 0.00)  
END LoadingandUnloadingcharges 
FROM GoodsReceivedNoteDetail GRND  
LEFT OUTER JOIN GoodsReceivedNote GRN ON GRN.GoodsReceivedNoteID=GRND.GoodsReceivedNoteID  
LEFT OUTER JOIN OtherExpenseCost OEC ON OEC.GoodsReceivedNoteID=GRN.GoodsReceivedNoteID) as sub

答案 1 :(得分:0)

为什么你使用select distinct和窗口函数,当好的老式聚合会这样做。然后,在SQL中,您无法在定义它的同一选择中重用别名。但是,您的逻辑很简单,可以重复(替代方法是子查询或CTE):

SELECT GRN.GoodsReceivedNoteNo,
       (CASE WHEN Count(GRND.GoodsReceivedNoteID) > 1
             THEN Isnull(( GRND.FreightCharges ), 0.00)  
             ELSE Isnull(( OEC.FreightCharges ), 0.00)
        END) FreightCharge,
       (CASE WHEN Count(GRND.GoodsReceivedNoteID) > 1
             THEN Isnull(( GRND.LoadingCost + GRND.UnloadingCost ), 0.00) 
             ELSE Isnull(( OEC.LoadingCost + OEC.UnloadingCost ), 0.00)
        END) LoadingandUnloadingcharges ,
       (CASE WHEN Count(GRND.GoodsReceivedNoteID) > 1
             THEN Isnull(( GRND.FreightCharges ), 0.00) + Isnull(( GRND.LoadingCost + GRND.UnloadingCost ), 0.00) 
             ELSE Isnull(( OEC.FreightCharges ), 0.00) + Isnull(( OEC.LoadingCost + OEC.UnloadingCost ), 0.00)
        END) FreightCharge,
FROM GoodsReceivedNoteDetail GRND LEFT OUTER JOIN
     GoodsReceivedNote GRN
     ON GRN.GoodsReceivedNoteID = GRND.GoodsReceivedNoteID LEFT OUTER JOIN 
     OtherExpenseCost OEC
     ON OEC.GoodsReceivedNoteID = GRN.GoodsReceivedNoteID   
GROUP BY GRN.GoodsReceivedNoteNo;

注意:如果两个表都可能有重复项,那么总和可能不是您想要的。也就是说,我不认为您的原始查询是合理的,但这不是您要问的问题。