带有透视

时间:2018-06-08 09:51:54

标签: sql dynamic pivot unpivot

enter image description here

我有图像中的数据..

类型将根据类型ID选择进行更改

我正在尝试实现和输出类似于以下内容: -

enter image description here

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 等等......

根据选择更改类型请帮助..

1 个答案:

答案 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. 从单个列(类型)执行多个枢轴(一个用于percount,一个用于daystotal)
  2. 使查询适用于动态sql
  3. 第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部分 - 使此查询动态化 需要动态定义以下项目 -

    1. 选择查询的标题 - 总和([166su01])[166su01],总和([166su01计数])[166su01计数],总和([166su02])[166su02],总和([166su02计数])[166su02计数]
    2. Percount Pivot - [166su01],[166su02]
    3. DaysTotal Pivot - [166su01 Count],[166su02 Count]
    4. 这可以通过 -

      来完成
      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)
      

      如果您觉得有用,请投票给答案