如何将第二行移到sql中的列?

时间:2018-08-16 01:45:30

标签: sql-server tsql sql-server-2012 pivot

我正在尝试了解PivotSQL的用例,并且想知道是否有人可以帮助我处理这组数据以获得所需的结果。

我有这组数据

Order_Day   Product_Id  SUM_RESULT
2011-07-01    P1           125
2011-07-02    P1           10
2011-07-01    P2           20
2011-07-02    P2           40
2011-07-01    P3           250
2011-07-02    P3           125
2011-07-02    P4           120
2011-07-02    P5           50
2011-07-02    P6           100

我试图得到如下所示的结果:

Product_Id  Day1Sale   Day2Sale
   P1          125        10       
   P2          20         40       
   P3          250        125
   P4          0          120
   P5          0          50
   P6          0          100

我尝试使用pivot来获得此结果,但由于经验不足而无法实现。

我将非常感谢您的帮助。

下面是创建表和数据集的代码

CREATE TABLE tblProduct ( Order_Day Datetime,
                            ORDER_ID nvarchar(50),
                            Product_Id nvarchar(10),
                            Quantity int,
                            Price int)

Insert into tblProduct (Order_Day,ORDER_ID,Product_Id,Quantity,price)
                Values( '1-jul-11','O1','P1',5,5),
                       ('1-jul-11','O2','P2',2,10),
                       ('1-jul-11','O3','P3',10,25),
                       ('1-jul-11','O4','P1',20,5),
                       ('2-jul-11','O5','P3',5,25),
                       ('2-jul-11','O6','P4',6,20),
                       ('2-jul-11','O7','P1',2,5),
                       ('2-jul-11','O8','P5',1,50),
                       ('2-jul-11','O9','P6',2,50),
                       ('2-jul-11','O10','P2',4,10)

3 个答案:

答案 0 :(得分:1)

SELECT
      Product_ID
    , ISNULL([2011-07-01], 0) AS Day1Sale
    , ISNULL([2011-07-02], 0) AS Day2Sale
FROM
    (SELECT Product_Id, Order_Day, Quantity * Price AS Sale FROM tblProduct) AS SourceTable
PIVOT
(
    SUM(Sale)
    FOR Order_Day IN ([2011-07-01], [2011-07-02])
) AS PivotTable

没有枢轴的替代解决方案:

SELECT
          Product_Id
        , SUM(Day1Sale) AS Day1Sale
        , SUM(Day2Sale) AS Day2Sale
    FROM
        (
            SELECT
                      Product_Id
                    , CASE WHEN Order_Day = '2011-07-01'
                        THEN ISNULL(SUM(Quantity * Price), 0)
                        ELSE 0
                      END AS Day1Sale
                    , CASE WHEN Order_Day = '2011-07-02'
                        THEN ISNULL(SUM(Quantity * Price), 0)
                        ELSE 0
                      END AS Day2Sale
                FROM
                    tblProduct
                GROUP BY
                    Product_Id, Order_Day
        ) AS SourceTable
    GROUP BY
        Product_Id

这2个解决方案使用硬编码日期。您可能需要查看dynamic pivot解决方案。

答案 1 :(得分:1)

就这样:

SELECT [Product_id] 
  ,SUM(IIF([Order_Day]= '2011-07-01', [SUM_RESULT], 0))
  ,SUM(IIF([Order_Day]= '2011-07-02', [SUM_RESULT], 0))
FROM @DataSource
GROUP BY [Product_id];

完整的示例:

DECLARE @DataSource TABLE
(
    [Order_Day] DATETIME
   ,[Product_id] VARCHAR(4)
   ,[SUM_RESULT] INT
);

INSERT INTO @DataSource ([Order_Day], [Product_id], [SUM_RESULT])
VALUES ('2011-07-01', 'P1', 125)
      ,('2011-07-02', 'P1', 10)
      ,('2011-07-01', 'P2', 20)
      ,('2011-07-02', 'P2', 40)
      ,('2011-07-01', 'P3', 250)
      ,('2011-07-02', 'P3', 125)
      ,('2011-07-02', 'P4', 120)
      ,('2011-07-02', 'P5', 50)
      ,('2011-07-02', 'P6', 100);

SELECT [Product_id] 
      ,SUM(IIF([Order_Day]= '2011-07-01', [SUM_RESULT], 0))
      ,SUM(IIF([Order_Day]= '2011-07-02', [SUM_RESULT], 0))
FROM @DataSource
GROUP BY [Product_id];

enter image description here

答案 2 :(得分:1)

根据我的理解和您的pivot请求,我这样写:

select Product_Id, [2011-07-01 00:00:00.000] date1, [2011-07-02 00:00:00.000] date2 from(
 select product_id, (price * quantity) priceQuantity, order_day from #tblProduct
) pivotTable
pivot
(
 sum(priceQuantity) for order_day in ([2011-07-01 00:00:00.000], [2011-07-02 00:00:00.000])
)sumTable

谢谢, 泰米尔语(TamilPugal)