2017-07-18 57 views
0

我想選擇dc.description不同的c。[weight]之和。我正在尋找5個案例,例如:dc.description如'failed%'然後計算c。[寄存參考]的總和。使用大小寫以檢索使用通配符的聚合

我會怎麼做呢?

SELECT lpr.RegID, lpr.Reg, 
         SUM(c.[Weight]) AS LeftOffWt 
       FROM dbo.whsCo wc 
         INNER JOIN whsHe wh on wh.WhsHeaderID = wc.whsHeaderID 
         INNER JOIN dbo.cgtCo c ON c.[Consignment Reference] = wc.ConsignmentReference 
         INNER JOIN dbo.disCo dc on dc.ConsignmentRef = wc.ConsignmentReference 
         INNER JOIN dbo.genA ga ON ga.AddressID = c.Consignor 
         INNER JOIN dbo.lpra lp ON lp.Postcode = ga.PostcodePrefix 
         INNER JOIN dbo.lstPos lpr ON lpr.RegionID = lp.RegionID 
                  AND c.Cancelled = 0 
       WHERE wc.StatusCode IN ('NL') and wh.ArrDepDate between @StartDate and @EndDate and dc.Description like '%failed%' 
       GROUP BY lpr.RegionID, lpr.Region 

回答

0

使用條件的聚集 - 即,使01​​的參數的聚合函數SUM()

SELECT lpr.RegID, lpr.Reg, 
     SUM(c.[Weight]) AS LeftOffWt, 
     SUM(CASE WHEN dc.description like 'failed%' THEN c.[consignment reference] ELSE 0 END) AS sum_failed, 
     . . . 
. . . 

您還需要從WHERE條款刪除的條件。

+0

我收到此錯誤:無法對包含聚合或子查詢的表達式執行聚合函數。 –

+0

@戴安娜。 。 。我的回答中沒有這樣的表達。這個代碼也沒有暗示任何這樣的表達。 –

相關問題