Reputation: 23
I am new to SQL pivot queries and stuck in Pivot of Pivot result.
I have one table named "ExampleSales". I want to find the monthly (or even daily, weekly, Monthly, Yearly) employee and item wise Sales. Below is the example of table, sample data and first level pivot.
CREATE TABLE [dbo].[ExampleSales](
[SalesId] [int] IDENTITY(1,1) NOT NULL,
[ItemCode] [varchar](50) NOT NULL,
[EmployeeName] [varchar](250) NOT NULL,
[Amount] [decimal](18, 2) NOT NULL,
[SalesDate] [datetime2](7) NOT NULL,
CONSTRAINT [PK_ExampleSales] PRIMARY KEY CLUSTERED
(
[SalesId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Sample data:
SET IDENTITY_INSERT [dbo].[ExampleSales] ON
GO
INSERT [dbo].[ExampleSales] ([SalesId], [ItemCode], [EmployeeName], [Amount], [SalesDate]) VALUES (1, N'A1', N'Michel John', CAST(120.00 AS Decimal(18, 2)), CAST(0x07207D922A5ADF3C0B AS DateTime2))
GO
INSERT [dbo].[ExampleSales] ([SalesId], [ItemCode], [EmployeeName], [Amount], [SalesDate]) VALUES (2, N'A1', N'Mark Ven', CAST(50.00 AS Decimal(18, 2)), CAST(0x07D07748385AF33C0B AS DateTime2))
GO
INSERT [dbo].[ExampleSales] ([SalesId], [ItemCode], [EmployeeName], [Amount], [SalesDate]) VALUES (3, N'A2', N'Michel John', CAST(50.00 AS Decimal(18, 2)), CAST(0x07F05983465A023D0B AS DateTime2))
GO
INSERT [dbo].[ExampleSales] ([SalesId], [ItemCode], [EmployeeName], [Amount], [SalesDate]) VALUES (4, N'A2', N'Mark Ven', CAST(150.00 AS Decimal(18, 2)), CAST(0x073054DF505AFD3C0B AS DateTime2))
GO
INSERT [dbo].[ExampleSales] ([SalesId], [ItemCode], [EmployeeName], [Amount], [SalesDate]) VALUES (5, N'A3', N'Michel John', CAST(23.00 AS Decimal(18, 2)), CAST(0x0730AC235F5ADF3C0B AS DateTime2))
GO
INSERT [dbo].[ExampleSales] ([SalesId], [ItemCode], [EmployeeName], [Amount], [SalesDate]) VALUES (6, N'A3', N'Mark Ven', CAST(56.00 AS Decimal(18, 2)), CAST(0x07F02161695AF33C0B AS DateTime2))
GO
INSERT [dbo].[ExampleSales] ([SalesId], [ItemCode], [EmployeeName], [Amount], [SalesDate]) VALUES (7, N'A4', N'Michel John', CAST(54.00 AS Decimal(18, 2)), CAST(0x07C09BB2825A023D0B AS DateTime2))
GO
INSERT [dbo].[ExampleSales] ([SalesId], [ItemCode], [EmployeeName], [Amount], [SalesDate]) VALUES (8, N'A4', N'Mark Ven', CAST(100.00 AS Decimal(18, 2)), CAST(0x0750622E925ADB3C0B AS DateTime2))
GO
SET IDENTITY_INSERT [dbo].[ExampleSales] OFF
GO
First level pivot query
SELECT
DateName( month , DateAdd( month , DATEPART(m,SalesDate) , 0 ) - 1 ) AS 'month',EmployeeName,[A1] ,[A2],[A3],[A4]
FROM
(SELECT
Salesid,SalesDate,EmployeeName,ItemCode
FROM
ExampleSales
) AS SourceTable
PIVOT
(
COUNT(Salesid)
FOR ItemCode IN([A1] ,[A2],[A3],[A4])
) AS PivotTable
Output of my first pivot query is
Now, I am stuck here. I want next level out as below.
Need covert first pivot to next level pivot as below
Upvotes: 2
Views: 121
Reputation: 1815
For dynamic one:
Use as follows:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(concat(c.employeename,'(',c.itemcode,')'))
FROM examplesales c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'select * from
(select salesid,datename(month,salesdate) as mn,concat(employeename,''('',itemcode,'')'') as items from examplesales
) as S
pivot
(
count(salesid)
for items in ('+@cols+') ) p '
execute(@query)
Upvotes: 0
Reputation: 1815
You can do this as follows:
select * from
(select salesid,datename(month,salesdate) as mn,concat(employeename,'(',itemcode,')') as items from examplesales
) as S
pivot
(
count(salesid)
for items in ([Michel John(A1)],[Mark Ven(A1)],[Michel John(A2)],[Mark Ven(A2)],
[Michel John(A3)],[Mark Ven(A3)],[Michel John(A4)],[Mark Ven(A4)])
) p
Result is :
Upvotes: 1