我是SQL数据透视查询的新手,并且陷入了Pivot of Pivot结果。
我有一个名为“ExampleSales”的表。我想找到每月(甚至每天,每周,每月,每年)员工和项目明智的销售。下面是表格,示例数据和第一级数据透视的示例。
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]
示例数据:
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
第一级数据透视查询
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
现在,我被困在这里。我希望下一级别如下。
答案 0 :(得分:1)
答案 1 :(得分:0)
对于动态的:
使用如下:
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)