返回带有数据透视表数据的多列

时间:2019-07-04 18:20:54

标签: sql sql-server database sql-server-2008

我有一个要求,我必须每月显示产品的总销售额,并通过如下所示的透视图完美地做到这一点:

SELECT *
FROM (SELECT m.ProductId [Product],  
       DATENAME(MONTH, m.OrderDate) [Month], 
       SUM(ISNULL(M.Quantity, 0)) [Sales]
      FROM SampleOrders m
      GROUP BY m.ProductId, 
      DATENAME(MONTH, m.OrderDate)) AS MontlySalesData
PIVOT( SUM([Sales])   
    FOR Month IN ([January],[February],[March],[April],[May],
    [June],[July],[August],[September],[October],[November],
    [December])) AS PivotData

输出

Product January February    March   April   May June    July    August  September   October November    December
1001    NULL    NULL    NULL    NULL    6   30  NULL    NULL    NULL    NULL    NULL    NULL
1002    NULL    NULL    NULL    NULL    14  6   NULL    NULL    NULL    NULL    NULL    NULL

因此在5月和6月,数据会使用 OrderDate 列按预期显示。但是我还有另一个要求,就是我还要显示来自另一个表的数据透视表的月度库存。为简单起见,我需要以下输出:

Product May May-Stock June June-Stock
1001    6   10        30   20
1002    14   6         6   10

我不确定如何实现此目标,如果一次可以使用两个支点来获得结果,我会感到有些困惑。任何想法将不胜感激。

NB :以下是带有示例数据的表模式

CREATE TABLE [dbo].[SampleOrders](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ProductId] [int] NULL,
    [OrderDate] [datetime] NULL,
    [CustomerId] [int] NULL,
    [Quantity] [float] NULL,
 CONSTRAINT [PK_SampleOrders] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Id  ProductId   OrderDate   CustomerId  Quantity
1   1001    2019-06-10 00:00:00.000 1   10
2   1001    2019-06-01 00:00:00.000 1   20
3   1002    2019-06-02 00:00:00.000 2   2
4   1002    2019-06-20 00:00:00.000 2   4
5   1001    2019-05-20 00:00:00.000 1   6
6   1002    2019-05-22 00:00:00.000 1   14

CREATE TABLE [dbo].[SampleStock](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ProductId] [int] NULL,
    [Quantity] [float] NULL,
    [Status] [int] NULL,
    [StockDate] [datetime] NULL,
 CONSTRAINT [PK_SampleStock] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Id  ProductId   Quantity    Status  StockDate
1   1001    20  1   2019-06-10 00:00:00.000
2   1002    10  1   2019-06-12 00:00:00.000
3   1001    10  1   2019-05-02 00:00:00.000
4   1002    4   2   2019-05-20 00:00:00.000

1 个答案:

答案 0 :(得分:2)

这可能会帮助

CREATE TABLE [dbo].[SampleOrders](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ProductId] [int] NULL,
    [OrderDate] [datetime] NULL,
    [CustomerId] [int] NULL,
    [Quantity] [float] NULL,
 CONSTRAINT [PK_SampleOrders] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[SampleStock](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ProductId] [int] NULL,
    [Quantity] [float] NULL,
    [Status] [int] NULL,
    [StockDate] [datetime] NULL,
 CONSTRAINT [PK_SampleStock] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

INSERT INTO SampleOrders(ProductId,OrderDate,CustomerId,Quantity) VALUES(1001,'2019-06-10 00:00:00.000', 1,   10)
INSERT INTO SampleOrders(ProductId,OrderDate,CustomerId,Quantity) VALUES(1001,'2019-06-01 00:00:00.000', 1,   20)
INSERT INTO SampleOrders(ProductId,OrderDate,CustomerId,Quantity) VALUES(1002,'2019-06-02 00:00:00.000', 2,   2 )
INSERT INTO SampleOrders(ProductId,OrderDate,CustomerId,Quantity) VALUES(1002,'2019-06-20 00:00:00.000', 2,   4 )
INSERT INTO SampleOrders(ProductId,OrderDate,CustomerId,Quantity) VALUES(1001,'2019-05-20 00:00:00.000', 1,   6 )
INSERT INTO SampleOrders(ProductId,OrderDate,CustomerId,Quantity) VALUES(1002,'2019-05-22 00:00:00.000', 1,   14)
GO
INSERT INTO SampleStock(ProductId,Quantity,Status,StockDate) VALUES (1001,20,1,'2019-06-10 00:00:00.000')
INSERT INTO SampleStock(ProductId,Quantity,Status,StockDate) VALUES (1002,10,1,'2019-06-12 00:00:00.000')
INSERT INTO SampleStock(ProductId,Quantity,Status,StockDate) VALUES (1001,10,1,'2019-05-02 00:00:00.000')
INSERT INTO SampleStock(ProductId,Quantity,Status,StockDate) VALUES (1002,6 ,2,'2019-05-20 00:00:00.000')
INSERT INTO SampleStock(ProductId,Quantity,Status,StockDate) VALUES (1003,4 ,2,'2019-05-20 00:00:00.000')
GO
SELECT * FROM SampleOrders
SELECT * FROM SampleStock
GO
-- DROP TABLE #MonthlySales
SELECT Product
      ,January as JanuarySales
      ,February as FebruarySales
      ,March as MarchSales
      ,April as AprilSales
      ,May as MaySales
      ,June as JuneSales
      ,July as JulySales
      ,August as AugustSales
      ,September as SeptemberSales
      ,October as OctoberSales
      ,November as NovemberSales
      ,December as DecemberSales
INTO #MonthlySales
FROM (SELECT m.ProductId [Product],  
       DATENAME(MONTH, m.OrderDate) [Month], 
       SUM(ISNULL(M.Quantity, 0)) [Sales]
      FROM SampleOrders m
      GROUP BY m.ProductId, 
      DATENAME(MONTH, m.OrderDate)) AS MontlySalesData
PIVOT( SUM([Sales])   
    FOR Month IN ([January],[February],[March],[April],[May],
    [June],[July],[August],[September],[October],[November],
    [December])) AS PivotData

GO
-- DROP TABLE #MonthlyStock
SELECT Product
      ,January as JanuaryStock 
      ,February as FebruaryStock
      ,March as MarchStock
      ,April as AprilStock
      ,May as MayStock
      ,June as JuneStock
      ,July as JulyStock
      ,August as AugustStock
      ,September as SeptemberStock
      ,October as OctoberStock
      ,November as NovemberStock
      ,December as DecemberStock
INTO #MonthlyStock
FROM (SELECT m.ProductId [Product],  
       DATENAME(MONTH, m.StockDate) [Month], 
       SUM(ISNULL(M.Quantity, 0)) [Stock] 
      FROM SampleStock m
      GROUP BY m.ProductId, 
      DATENAME(MONTH, m.StockDate)) AS MontlyStock
PIVOT( SUM([Stock])   
    FOR Month IN ([January],[February],[March],[April],[May],
    [June],[July],[August],[September],[October],[November],
    [December])) AS PivotData

GO
SELECT ISNULL(sl.Product,st.Product) as Product
      ,JanuarySales,JanuaryStock
      ,FebruarySales,FebruaryStock
      ,MarchSales,MarchStock
      ,AprilSales,AprilStock
      ,MaySales,MayStock
      ,JuneSales,JuneStock
      ,JulySales,JulyStock
      ,AugustSales,AugustStock
      ,SeptemberSales,SeptemberStock
      ,OctoberSales,OctoberStock
      ,NovemberSales,NovemberStock
      ,DecemberSales,DecemberStock
FROM #MonthlySales sl
FULL JOIN #MonthlyStock st ON sl.Product = st.Product