我的Pivot功能有问题,但实际上我不知道为什么

时间:2017-11-04 00:55:27

标签: sql-server

我的表包含Order_Day,Product_ID,Quantity和Price列:

create table dbo.AmazonTestTable
(order_day date,
order_id  int,
product_id char(2),
quantity int,
price int)
go

insert into dbo.AmazonTestTable values
('01-JUL-11',1,'p1',5,5),
('01-JUL-11',2,'p2',2,10),
('01-JUL-11',3,'p3',10,25),
('01-JUL-11',4,'p1',20,5),
('02-JUL-11',5,'p3',5,25),
('02-JUL-11',6,'p4',6,20),
('02-JUL-11',7,'p1',2,5),
('02-JUL-11',8,'p5',1,50),
('02-JUL-11',9,'p6',2,50),
('02-JUL-11',10,'p2',4,10)
go

问题在于我想选择order_day,product_id和TotalSales(这可以通过按价格使用数量来计算)然后转动它。因此,旋转表应该有三列:product_id,'01 -JUL-11','02-JUL-11'。所以我使用下面的代码:

select product_id,'2011-07-01' as TotalSalesOnDay1,'2011-07-02' as 
TotalSalesOnDay2
from (select product_id,order_day,(quantity*price) as TotalSales
      from dbo.AmazonTestTable) as TotalSalesPerDay
pivot
(sum(TotalSales) for [order_day] in ([2011-07-01] ,[2011-07-02] )) as 
PivotDataSet;
go

但它返回如下结果:

enter image description here

4 个答案:

答案 0 :(得分:2)

'2011-07-01'是一个常量字符串。 SQL Server中的转义方法使用方括号:

select product_id, [2011-07-01] as TotalSalesOnDay1, [2011-07-02] as TotalSalesOnDay2
. . . 

答案 1 :(得分:1)

这个查询可以解决问题 -

;With PivotData
As
(
    Select 
        product_id, -- grouping element
        order_day, -- spreading element
        (quantity * price) As TotalSales -- aggregating element
    From AmazonTestTable
)
Select
    product_id, [2011-07-01] As TotalSalesDay1, [2011-07-02] As TotalSalesDay2
From PivotData
Pivot (Sum(TotalSales) For order_day In ([2011-07-01], [2011-07-02])) As P

它将为您提供以下输出 -

product_id  TotalSalesDay1  TotalSalesDay2
p1          125             10
p2          20              40
p4          NULL            120
p3          250             125
p5          NULL            50
p6          NULL            100

答案 2 :(得分:0)

你想要这样的东西

    SELECT product_id, order_day, SUM(TotalSales) AS TotalSales FROM 
       (SELECT product_id,order_day,(quantity*price) as TotalSales
       FROM AmazonTestTable) AS TotalSalesPerDay
    GROUP BY TotalSalesPerDay.order_day;

输出将是:

product_id| order_day | TotalSales
p1        | 01-JUL-11 |395
p2        | 02-JUL-11 |445

答案 3 :(得分:0)

过去几个月我在这里看到了数百个支点问题。让我发布一些示例解决方案,也许可以帮助其他人学习基础知识,因此我们不会一遍又一遍地看到同样的事情。

CREATE TABLE Products (
 product VARCHAR(30),
 market_year INT,
 value INT,
 quantity INT,
 CONSTRAINT pk_products
 PRIMARY KEY (product, market_year));

INSERT INTO Products VALUES('Corn', 2003, 100, 20);
INSERT INTO Products VALUES('Corn', 2004, 200, 25);
INSERT INTO Products VALUES('Corn', 2005, 150, 30);
INSERT INTO Products VALUES('Corn', 2006, 150, 10);

SELECT product,
       SUM(CASE WHEN market_year = 2003 THEN value ELSE 0 END) AS v2003,
       SUM(CASE WHEN market_year = 2003 THEN quantity ELSE 0 END) AS q2003,
       SUM(CASE WHEN market_year = 2004 THEN value ELSE 0 END) AS v2004,
       SUM(CASE WHEN market_year = 2004 THEN quantity ELSE 0 END) AS q2004,
       SUM(CASE WHEN market_year = 2005 THEN value ELSE 0 END) AS v2005,
       SUM(CASE WHEN market_year = 2005 THEN quantity ELSE 0 END) AS q2005,
       SUM(CASE WHEN market_year = 2006 THEN value ELSE 0 END) AS v2006,
       SUM(CASE WHEN market_year = 2006 THEN quantity ELSE 0 END) AS q2006
FROM Products
GROUP BY product;

结果:

product v2003   q2003   v2004   q2004   v2005   q2005   v2006   q2006
Corn    100 20  200 25  150 30  150 10

另外。 。 。

USE AdventureWorks
GO
-- Creating Test Table
CREATE TABLE Product(Cust VARCHAR(25), Product VARCHAR(20), QTY INT)
GO
-- Inserting Data into Table
INSERT INTO Product(Cust, Product, QTY)
VALUES('KATE','VEG',2)
INSERT INTO Product(Cust, Product, QTY)
VALUES('KATE','SODA',6)
INSERT INTO Product(Cust, Product, QTY)
VALUES('KATE','MILK',1)
INSERT INTO Product(Cust, Product, QTY)
VALUES('KATE','BEER',12)
INSERT INTO Product(Cust, Product, QTY)
VALUES('FRED','MILK',3)
INSERT INTO Product(Cust, Product, QTY)
VALUES('FRED','BEER',24)
INSERT INTO Product(Cust, Product, QTY)
VALUES('KATE','VEG',3)
GO
-- Selecting and checking entires in table
SELECT *
FROM Product
GO
-- Pivot Table ordered by PRODUCT
SELECT PRODUCT, FRED, KATE
FROM (
SELECT CUST, PRODUCT, QTY
FROM Product) up
PIVOT (SUM(QTY) FOR CUST IN (FRED, KATE)) AS pvt
ORDER BY PRODUCT
GO
-- Pivot Table ordered by CUST
SELECT CUST, VEG, SODA, MILK, BEER, CHIPS
FROM (
SELECT CUST, PRODUCT, QTY
FROM Product) up
PIVOT (SUM(QTY) FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)) AS pvt
ORDER BY CUST
GO
-- Unpivot Table ordered by CUST
SELECT CUST, PRODUCT, QTY
FROM
(
SELECT CUST, VEG, SODA, MILK, BEER, CHIPS
FROM (
SELECT CUST, PRODUCT, QTY
FROM Product) up
PIVOT
( SUM(QTY) FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)) AS pvt) p
UNPIVOT
(QTY FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)
) AS Unpvt
GO
-- Clean up database
DROP TABLE Product
GO

ResultSet:
-- Selecting and checking entires in table
Cust Product QTY
------------------------- -------------------- -----------
KATE VEG 2
KATE SODA 6
KATE MILK 1
KATE BEER 12
FRED MILK 3
FRED BEER 24
KATE VEG 3

-- Pivot Table ordered by PRODUCT
PRODUCT FRED KATE
-------------------- ----------- -----------
BEER 24 12
MILK 3 1
SODA NULL 6
VEG NULL 5

-- Pivot Table ordered by CUST
CUST VEG SODA MILK BEER CHIPS
------------------------- ----------- ----------- ----------- ----------- -----------
FRED NULL NULL 3 24 NULL
KATE 5 6 1 12 NULL

-- Unpivot Table ordered by CUST
CUST PRODUCT QTY
------------------------- -------- -----------
FRED MILK 3
FRED BEER 24
KATE VEG 5
KATE SODA 6
KATE MILK 1
KATE BEER 12 12

如果您事先不知道将要处理的所有字段,您甚至可以进行动态调整。让SQL Server为您完成工作!!

--Create Temporary Table #CourseSales
CREATE TABLE #CourseSales
(Course VARCHAR(50),Year INT,Earning MONEY)
GO
--Populate Sample records
INSERT INTO #CourseSales VALUES('.NET',2012,10000)
INSERT INTO #CourseSales VALUES('Java',2012,20000)
INSERT INTO #CourseSales VALUES('.NET',2012,5000)
INSERT INTO #CourseSales VALUES('.NET',2013,48000)
INSERT INTO #CourseSales VALUES('Java',2013,30000)
GO

SELECT *
FROM #CourseSales
PIVOT(SUM(Earning) 
      FOR Course IN ([.NET], Java)) AS PVTTable

你会在这里找到一些很棒的资源!

http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/understanding-sql-server-2000-pivot/

http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/dynamic-pivot-on-multiple-columns/