0

私はこのテーブルを持っています:

idSection   | idQuestion    | title     | enunciation | total   | name 
1           | 1             | title 1   | question 1    | 5     | Good
1           | 1             | title 1   | question 1    | 3     | Very Good
1           | 1             | title 1   | question 1    | 1     | Bad
1           | 2             | title 2   | question 2    | 1     | Good
1           | 2             | title 2   | question 2    | 3     | Bad
1           | 3             | title 3   | question 3    | 1     | Bad

複数の列で列の値に名前を付けるにはどうすればよいですか?

期待される結果は次のとおりです。

title   | enunciantion  | bad   | good  | very good 
title 1 | question 1    | 1     | 5     | 3
title 2 | question 2    | 3     | 1     | 0
title 3 | question 3    | 1     | 0     | 0

列がいくつあるかわかりません(動的です)。

新しい編集:

 title      |  enunciantion | Column0  | Column1 | Column2 | ... | Column7 |
   0        |   0           | bad      |  good   |very good| ... |    0    |
title 1     | question 1    | 1        | 5       | 3       | ... |    0    |
title 2     | question 2    | 3        | 1       | 0       | ... |    0    |
title 3     | question 3    | 1        | 0       | 0       | ... |    5    |

さて、唯一の違いは、動的な列ではなく静的な列しかないことですが、テーブルの最初の行は、別のテーブルを見るような動的な値です。

4

2 に答える 2

3

MySQLの場合:

SELECT
  title,
  enunciantion,
  SUM(CASE WHEN name = 'bad' THEN total ELSE 0 END) AS 'bad',
  SUM(CASE WHEN name = 'good' THEN total ELSE 0 END) AS 'good',
  SUM(CASE WHEN name = 'very good ' THEN total ELSE 0 END) AS 'very good'
FROM Tablename
GROUP BY  title,
          enunciantion;

SQLフィドルデモ

SQL Serverの場合:

SELECT
  title,
  enunciation,
  bad,
  good,
  [very good]
FROM Table1
PIVOT
(
  SUM(total) FOR name IN([good], [bad], [very good])
) p;

SQLFiddleデモSQLServer


アップデート:

これらの値good, bad, very goodが別のテーブルから来ており、これを動的に実行したい場合。

MySQLの場合:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT CONCAT('SUM(IF(Name = ''',
      Name, ''', Total, 0)) AS ',
      '''',Name, '''')
  ) INTO @sql
FROM names;


SET @sql = CONCAT('SELECT title, enunciation, ',
                  @sql,
                  'FROM Table1 GROUP BY  title, '
                  '  enunciation; ');
prepare stmt 
FROM @sql;

execute stmt;

SQL FiddleデモMySQL、動的

|   TITLE | ENUNCIATION | GOOD | VERY GOOD | BAD | VERY BAD |
-------------------------------------------------------------
| title 1 |  question 1 |    5 |         3 |   1 |        0 |
| title 2 |  question 2 |    1 |         0 |   3 |        0 |
| title 3 |  question 3 |    0 |         0 |   1 |        0 |

SQL Serverの場合、これを行うことができます。

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

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.name) 
                    from names c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

SET @query = 'SELECT  title, enunciation, ' + @cols +
              ' FROM (SELECT title, enunciation, total, name
      FROM Table1) t
PIVOT
(
  SUM(total) FOR name IN( ' + @cols + + ' )) p;';

execute(@query);

SQLFiddleデモSQLServer 、動的

これはあなたに与えるでしょう:

|   TITLE | ENUNCIATION | BAD |   GOOD | VERY BAD | VERY GOOD |
---------------------------------------------------------------
| title 1 |  question 1 |   1 |      5 |   (null) |         3 |
| title 2 |  question 2 |   3 |      1 |   (null) |    (null) |
| title 3 |  question 3 |   1 | (null) |   (null) |    (null) |
于 2013-01-10T11:54:13.410 に答える
3

SQLサーバーの動的SQLバージョンに関する@MahmoudGamalの回答を拡張するだけです。値をゼロに置き換える場合nullは、次を使用できます。

DECLARE @colsPivot AS NVARCHAR(MAX),
    @colsNull AS NVARCHAR(MAX),
    @query AS NVARCHAR(MAX);

select @colsPivot = STUFF((SELECT distinct ',' + QUOTENAME(c.name) 
                    from names c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

select @colsNull 
    = STUFF((SELECT distinct ', IsNull(' + QUOTENAME(c.name) + ', 0) as '+ QUOTENAME(c.name)
             from names c
             FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

SET @query = 'SELECT  title, enunciation, ' + @colsNull +
             ' FROM 
              (
                SELECT title, enunciation, total, name
                FROM results
              ) t
              PIVOT
              (
                  SUM(total) 
                  FOR name IN( ' + @colsPivot + + ' )
              ) p;';

exec(@query);

SQL FiddlewithDemoを参照してください

結果は次のとおりです。

|   TITLE | ENUNCIATION | BAD | GOOD | VERY BAD | VERY GOOD |
-------------------------------------------------------------
| title 1 |  question 1 |   1 |    5 |        0 |         3 |
| title 2 |  question 2 |   3 |    1 |        0 |         0 |
| title 3 |  question 3 |   1 |    0 |        0 |         0 |
于 2013-01-10T12:49:21.917 に答える