SQL Server透视麻烦

时间:2014-12-19 10:32:45

标签: sql sql-server pivot

有没有人知道如何转动此查询,以便Manufacturer.name为列,行为月份,这些是在子查询中计算的。

SELECT Manufacturer.manufacturer_id
      ,Manufacturer.name
      ,(SELECT ISNULL(SUM(SalesOrderLineItem.price*SalesOrderLineItem.quantity), 0) 
        FROM SalesOrderLineItem 
        INNER JOIN Products 
            ON SalesOrderLineItem.product_id = Products.product_id 
        INNER JOIN SalesOrder 
            ON SalesOrderLineItem.order_id = SalesOrder.sales_order_id 
        INNER JOIN Client 
            ON SalesOrder.client_id = Client.client_id 
        WHERE products.manufacturer_id = Manufacturer.manufacturer_id 
          AND Client.client_id = '224' 
          AND SalesOrder.order_placed BETWEEN '2014-09-01 00:00:00' AND '2014-09-30 23:59:00') AS 'sep-14'
      ,(SELECT ISNULL(SUM(SalesOrderLineItem.price*SalesOrderLineItem.quantity), 0) 
        FROM SalesOrderLineItem 
        INNER JOIN Products 
            ON SalesOrderLineItem.product_id = Products.product_id 
        INNER JOIN SalesOrder 
            ON SalesOrderLineItem.order_id = SalesOrder.sales_order_id 
        INNER JOIN Client 
            ON SalesOrder.client_id = Client.client_id 
        WHERE products.manufacturer_id = Manufacturer.manufacturer_id 
          AND Client.client_id = '224' 
          AND SalesOrder.order_placed BETWEEN '2014-10-01 00:00:00' AND '2014-10-31 23:59:00') AS 'oct-14'
      ,(SELECT ISNULL(SUM(SalesOrderLineItem.price*SalesOrderLineItem.quantity), 0) 
        FROM SalesOrderLineItem 
        INNER JOIN Products 
            ON SalesOrderLineItem.product_id = Products.product_id 
        INNER JOIN SalesOrder 
            ON SalesOrderLineItem.order_id = SalesOrder.sales_order_id 
        INNER JOIN Client ON SalesOrder.client_id = Client.client_id 
        WHERE products.manufacturer_id = Manufacturer.manufacturer_id 
          AND Client.client_id = '224' 
          AND SalesOrder.order_placed BETWEEN '2014-11-01 00:00:00' AND '2014-11-30 23:59:00') AS 'nov-14'
      ,(SELECT ISNULL(SUM(SalesOrderLineItem.price*SalesOrderLineItem.quantity), 0) 
        FROM SalesOrderLineItem 
        INNER JOIN Products 
            ON SalesOrderLineItem.product_id = Products.product_id 
        INNER JOIN SalesOrder 
            ON SalesOrderLineItem.order_id = SalesOrder.sales_order_id 
        INNER JOIN Client 
            ON SalesOrder.client_id = Client.client_id 
        WHERE products.manufacturer_id = Manufacturer.manufacturer_id 
          AND Client.client_id = '224' 
          AND SalesOrder.order_placed BETWEEN '2014-12-01 00:00:00' AND '2014-12-31 23:59:00') AS 'dec-14'
FROM Manufacturer

非常感谢。

1 个答案:

答案 0 :(得分:1)

试一试:

WITH DATA
AS
(
    SELECT Manufacturer.name AS ManufacturerName
          ,MONTH(SalesOrder.order_placed) AS MonthNumber
          ,ISNULL(SUM(SalesOrderLineItem.price*SalesOrderLineItem.quantity), 0) AS Total
    FROM SalesOrderLineItem 
    INNER JOIN Products 
        ON SalesOrderLineItem.product_id = Products.product_id 
    INNER JOIN SalesOrder 
        ON SalesOrderLineItem.order_id = SalesOrder.sales_order_id 
    INNER JOIN Client 
        ON SalesOrder.client_id = Client.client_id 
    INNER JOIN Manufacturer
        ON products.manufacturer_id = Manufacturer.manufacturer_id 
       AND Client.client_id = '224' 
       AND SalesOrder.order_placed BETWEEN '2014-09-01 00:00:00' AND '2014-12-31 23:59:00'
)
SELECT MonthNumber, [ManufacturerName1], [ManufacturerName2]
FROM DATA
PIVOT (MAX(Total) FOR ManufacturerName IN ([ManufacturerName1], [ManufacturerName2])) AS P;

不幸的是,您必须在查询中硬编码制造商名称,或者为未知制造商使用动态SQL。