简体   繁体   English

返回带有枢轴数据的多列

[英]Return Multi-Column With Pivot Data

I've a requirement where I've to show total sales of a product month-wise and did it perfectly with pivot as follows:我有一个要求,我必须按月显示产品的总销售额,并使用枢轴完美地完成,如下所示:

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

Output :输出

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

So in May and June, the data are shown as expected using the OrderDate column.因此,在 5 月和 6 月,数据使用OrderDate列按预期显示。 But I've another requirement where I've to show the month-wise stock as well with the pivot data from another table.但我还有另一个要求,我必须用另一个表中的数据透视表显示每月的库存。 To make it simple, I require something as below output:为了简单起见,我需要如下输出:

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

I am not sure how I can achieve this and bit confused if I can use two pivots at a time to get the result.如果我可以一次使用两个支点来获得结果,我不确定如何实现这一点并且有点困惑。 Any ideas would be highly appreciated.任何想法将不胜感激。

NB : Below are the tables schemas with sample data注意:以下是带有示例数据的表模式

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

This may help这可能有帮助

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM