Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在sql中将第二行移动到第二列?_Sql Server_Tsql_Sql Server 2012_Pivot - Fatal编程技术网

Sql server 如何在sql中将第二行移动到第二列?

Sql server 如何在sql中将第二行移动到第二列?,sql-server,tsql,sql-server-2012,pivot,Sql Server,Tsql,Sql Server 2012,Pivot,我试图理解SQL中Pivot的用例,想知道是否有人可以帮助我处理这组数据,以获得我需要的结果 我有这组数据 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-0

我试图理解
SQL
Pivot
的用例,想知道是否有人可以帮助我处理这组数据,以获得我需要的结果

我有这组数据

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)
无枢轴的替代解决方案:

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
这两种解决方案使用硬编码日期。您可能需要查看解决方案。

仅此:

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];

根据我的理解和您的
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
谢谢, 泰米尔普加尔

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