sql server pivot : group by with dynamic columns

你离开我真会死。 提交于 2019-12-19 11:32:15

问题


I have done a pivot on a table, generating dynamic columns :

DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct ',' +
                        QUOTENAME(Replace(variable,char(CAST(0x0016 as int)),'')) val
                      FROM TABLEDATA
                      ORDER BY val asc
                      FOR XML PATH(''), TYPE
                     ).value('.', 'NVARCHAR(MAX)') 
                        , 1, 1, '');

SELECT @query =

'SELECT
  time,
  country,
  disease,
  ' + @cols + '
FROM
  TABLEDATA
PIVOT 
(
  MAX(value) 
  FOR variable IN( ' + @cols + ' )' +
' ) AS p; ';

 execute(@query);

It returns the following result :

time | country | disease | indicateur1 | indicateur2 | ... | indicateur14 | ... | indicateurn
-----------------------------------------------------------------------------------------
2001 | FRANCE  | MAL     | 0.003       |             |     |              |     |
2002 | FRANCE  | AID     |             | 0.45        |     |              |     |
1997 | ITALY   | HIV     | 0.307       |             |     |              |     |
1999 | ROMANIA | MAL     | 1.044       |             |     |              |     |
2003 | ROMANIA | AID     |             |             |     | 0.77         |     |
2003 | ROMANIA | AID     | 0.872       |             |     |              |     |
2004 | ROMANIA | HIV     |             | 0.921       |     |              |     |
2002 | IRELAND | MAL     |             | 0.008       |     |              |     | 

But I need to group by the first 3 columns to get the following :

time | country | disease | indicateur1 | indicateur2 | ... | indicateur14 | ... | indicateurn
-----------------------------------------------------------------------------------------
2001 | FRANCE  | MAL     | 0.003       |             |     |              |     |
2002 | FRANCE  | AID     |             | 0.45        |     |              |     |
1997 | ITALY   | HIV     | 0.307       |             |     |              |     |
1999 | ROMANIA | MAL     | 1.044       |             |     |              |     |
2003 | ROMANIA | AID     | 0.872       |             |     | 0.77         |     |
2004 | ROMANIA | HIV     |             | 0.921       |     |              |     |
2002 | IRELAND | MAL     |             | 0.008       |     |              |     |

So I need to group by time, country, disease, but as my columns from the pivot are dynamics, I can't manage to find a solution...

Here is an SQL fiddle


回答1:


DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct ',' +
                        QUOTENAME(Replace(variable,char(CAST(0x0016 as int)),'')) val
                      FROM TABLEDATA
                      ORDER BY val asc
                      FOR XML PATH(''), TYPE
                     ).value('.', 'NVARCHAR(MAX)') 
                        , 1, 1, '');

SELECT @query =

'SELECT
  time,
  country,
  disease,
  ' + @cols + '
FROM (
    Select
        time,
        country,
        disease,
        variable,
        convert(decimal(10, 2), value) as value
    From
        TABLEDATA
    ) t
PIVOT 
(
  Sum(value)
  FOR variable IN( ' + @cols + ' )' +
' ) AS p; ';


Execute(@query);

Example Fiddle



来源:https://stackoverflow.com/questions/18914735/sql-server-pivot-group-by-with-dynamic-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!