如何在列上获取没有组的信息

时间:2016-06-16 07:07:49

标签: sql sql-server tsql

我需要根据SUM(d.OrderQty)获得热门,但我还需要根据特定过滤器(OrderSourceID)获取信息。

当我将它添加到组时,它会更改顺序逻辑并返回相同数字的多行。执行此SQL的正确方法是什么?

SELECT * 
FROM
    (SELECT TOP (@NumOfRecords) 
         i.Number as Number,
         i.Description as Description,
         SUM(d.OrderQty) as TotalCount,
         CASE 
            WHEN h.OrderSourceID = 1 THEN SUM(d.OrderQty) 
            ELSE 0 
         END as TotalSource_1,
         CASE 
            WHEN h.OrderSourceID = 2 THEN SUM(d.OrderQty) 
            ELSE 0 
         END as TotalSource_2,
         CASE 
            WHEN h.OrderSourceID = 3 THEN SUM(d.OrderQty) 
            ELSE 0 
         END as TotalSource_3 
     FROM 
         tblHeader h
     INNER JOIN 
         tblDetail d on h.SalesOrderID = d.SalesOrderID
     INNER JOIN 
         tblItemDetail id on id.ItemID = d.ItemID
     INNER JOIN 
         tblItem i on d.ItemID = i.ItemID
     WHERE
         CONVERT(DATE, h.OrderDate) BETWEEN @StartDate AND @EndDate
     GROUP BY
         id.ItemID, i.Number, i.Description, h.OrderSourceID
     ORDER BY
         SUM(d.OrderQty) DESC) x
ORDER BY
    x.Number

2 个答案:

答案 0 :(得分:1)

SELECT * 
FROM
    (SELECT TOP (@NumOfRecords) 
         i.Number as Number,
         i.Description as Description,
         SUM(d.OrderQty) as TotalCount,
         SUM(CASE 
            WHEN h.OrderSourceID = 1 THEN d.OrderQty 
            ELSE 0 
         END) as TotalSource_1,
         SUM(CASE 
            WHEN h.OrderSourceID = 2 THEN d.OrderQty
            ELSE 0 
         END) as TotalSource_2,
         SUM(CASE 
            WHEN h.OrderSourceID = 3 THEN d.OrderQty
            ELSE 0 
         END) as TotalSource_3 
     FROM 
         tblHeader h
     INNER JOIN 
         tblDetail d on h.SalesOrderID = d.SalesOrderID
     INNER JOIN 
         tblItemDetail id on id.ItemID = d.ItemID
     INNER JOIN 
         tblItem i on d.ItemID = i.ItemID
     WHERE
         CONVERT(DATE, h.OrderDate) BETWEEN @StartDate AND @EndDate
     GROUP BY
         id.ItemID, i.Number, i.Description, h.OrderSourceID
     ORDER BY
         SUM(d.OrderQty) DESC) x
ORDER BY
    x.Number

答案 1 :(得分:1)

如果我已经正确理解了这个问题,那么你只需要在SUM内部移动对h.OrderSourceID的引用,就像这样;

SELECT * 
FROM
    (SELECT TOP (@NumOfRecords) 
         i.Number as Number,
         i.Description as Description,
         SUM(d.OrderQty) as TotalCount,
         SUM(CASE WHEN h.OrderSourceID = 1 THEN d.OrderQty ELSE 0 END) as TotalSource_1,
         SUM(CASE WHEN h.OrderSourceID = 2 THEN d.OrderQty ELSE 0 END) as TotalSource_2,
         SUM(CASE WHEN h.OrderSourceID = 3 THEN d.OrderQty ELSE 0 END) as TotalSource_3
     FROM 
         tblHeader h
     INNER JOIN 
         tblDetail d on h.SalesOrderID = d.SalesOrderID
     INNER JOIN 
         tblItemDetail id on id.ItemID = d.ItemID
     INNER JOIN 
         tblItem i on d.ItemID = i.ItemID
     WHERE
         CONVERT(DATE, h.OrderDate) BETWEEN @StartDate AND @EndDate
     GROUP BY
         id.ItemID, i.Number, i.Description
     ORDER BY
         SUM(d.OrderQty) DESC) x
ORDER BY
    x.Number