我有图像中的数据..
类型将根据类型ID选择进行更改
166su01count对应于166su01类型,它应该显示日期4/16/2018的5(daystotal)
166su01Rate对应于166su01类型,它应显示日期4/16/2018的60.00(percount)
166su02count对应于166su02类型,它应显示日期4/16/2018的7(daystotal)
166su02Rate对应于166su02类型,它应该显示28.57(percount)的日期4/16/2018 等等......
根据选择更改类型请帮助..
答案 0 :(得分:0)
为MS-SQL Server提供解决方案 -
创建表格和数据 -
CREATE TABLE [dbo].[Type](
[daynumbers] [date] NULL,
[daystotal] [int] NULL,
[types] [varchar](15) NULL,
[percount] [float] NULL
) ON [PRIMARY]
INSERT INTO [dbo].[Type]
([daynumbers]
,[daystotal]
,[types]
,[percount])
VALUES
('2018-04-16', 5 ,'166su01', 60)
,('2018-04-16', 7 ,'166su02', 28.57)
,('2018-04-18', 8 ,'166su02', 62.5)
,('2018-04-19', 5 ,'166su01', 100)
问题分为两部分 -
第1部分 -
这可以通过复制类型列并将其命名为types1并将表旋转两次来解决 - 一个用于percount,一个用于daystotal
SELECT *
FROM
(SELECT *,types+' Count'[types1] FROM dbo.Type) AS SourceTable --duplicate column created here
PIVOT
(
SUM(percount)
FOR types IN ([166su01], [166su02])
) AS PivotTable
PIVOT
(
SUM(daystotal)
FOR types1 IN ([166su01 Count], [166su02 Count])
) AS PivotTable
这将创建额外的行 因此,按照以下方式使用daynumbers列对它们进行分组 -
SELECT daynumbers,Sum([166su01])[166su01],Sum( [166su01 Count]) [166su01 Count], Sum([166su02])[166su02],Sum( [166su02 Count]) [166su02 Count] FROM
(SELECT *,types+' Count'[types1] FROM dbo.Type) AS SourceTable
PIVOT
(
SUM(percount)
FOR types IN ([166su01], [166su02])
) AS PivotTable
PIVOT
(
SUM(daystotal)
FOR types1 IN ([166su01 Count], [166su02 Count])
) AS PivotTable
group by
daynumbers
第2部分 - 使此查询动态化 需要动态定义以下项目 -
这可以通过 -
来完成DECLARE @PivotPercount NVARCHAR(MAX) = N'' --Variable to hold types to pivot for percount
SELECT @PivotPercount = @PivotPercount + ', [' + COALESCE(types, '') + ']' FROM (SELECT DISTINCT types FROM dbo.Type)DT
SELECT @PivotPercount = LTRIM(STUFF(@PivotPercount, 1, 1, '')) --Remove first comma and space
print @PivotPercount
DECLARE @PivotDaysTotal NVARCHAR(MAX) = N'' --Variable to hold types to pivot for daystotal
SELECT @PivotDaysTotal = @PivotDaysTotal + ', [' + COALESCE(types, '') + ' Count]' FROM (SELECT DISTINCT types FROM dbo.Type)DT
SELECT @PivotDaysTotal = LTRIM(STUFF(@PivotDaysTotal, 1, 1, '')) --Remove first comma and space
print @PivotDaysTotal
DECLARE @Headers NVARCHAR(MAX) = N'' --Variable to hold unique customers to be used in PIVOT clause
SELECT @Headers = @Headers + ', Sum([' + COALESCE(types, '') + '])'+'[' + COALESCE(types, '') + ']' + ',Sum( [' + COALESCE(types, '') + ' Count]) ' + '[' + COALESCE(types, '') + ' Count]' FROM (SELECT DISTINCT types FROM dbo.Type)DT
SELECT @Headers = LTRIM(STUFF(@Headers, 1, 1, '')) --Remove first comma and space
print @Headers
现在结合一切 - 最终查询 -
DECLARE @PivotPercount NVARCHAR(MAX) = N'' --Variable to hold types to pivot for percount
SELECT @PivotPercount = @PivotPercount + ', [' + COALESCE(types, '') + ']' FROM (SELECT DISTINCT types FROM dbo.Type)DT
SELECT @PivotPercount = LTRIM(STUFF(@PivotPercount, 1, 1, '')) --Remove first comma and space
print @PivotPercount
DECLARE @PivotDaysTotal NVARCHAR(MAX) = N'' --Variable to hold types to pivot for daystotal
SELECT @PivotDaysTotal = @PivotDaysTotal + ', [' + COALESCE(types, '') + ' Count]' FROM (SELECT DISTINCT types FROM dbo.Type)DT
SELECT @PivotDaysTotal = LTRIM(STUFF(@PivotDaysTotal, 1, 1, '')) --Remove first comma and space
print @PivotDaysTotal
DECLARE @Headers NVARCHAR(MAX) = N'' --Variable to hold unique customers to be used in PIVOT clause
SELECT @Headers = @Headers + ', Sum([' + COALESCE(types, '') + '])'+'[' + COALESCE(types, '') + ']' + ',Sum( [' + COALESCE(types, '') + ' Count]) ' + '[' + COALESCE(types, '') + ' Count]' FROM (SELECT DISTINCT types FROM dbo.Type)DT
SELECT @Headers = LTRIM(STUFF(@Headers, 1, 1, '')) --Remove first comma and space
print @Headers
DECLARE @SQLStatement NVARCHAR(MAX) = N'' --Variable to hold t-sql query
--Generate dynamic PIVOT query here
SET @SQLStatement =
N'SELECT daynumbers,' + @Headers +
' FROM
(SELECT *,types+'' Count''[types1] FROM dbo.Type) AS SourceTable
PIVOT
(
SUM(percount)
FOR types IN ('+ @PivotPercount +')
) AS PivotTable
PIVOT
(
SUM(daystotal)
FOR types1 IN (' + @PivotDaysTotal + ')
) AS PivotTable
group by
daynumbers
'
print @SQLStatement
--Execute the dynamic t-sql PIVOT query below
EXEC (@SQLStatement)
如果您觉得有用,请投票给答案