このタイプのデータ変換はピボットです。MySQL でこの結果を生成するには、次のcase
式で集計関数を使用します。
select name,
sum(case when country_id = 'uk' then n_ocurrences else 0 end) occurrences_uk,
sum(case when country_id = 'us' then n_ocurrences else 0 end) occurrences_us,
sum(n_ocurrences) total_ocurrences
from yourtable
group by name
SQL Fiddle with Demoを参照してください。
上記のバージョンは、country_id
事前に値がわかっている場合にうまく機能しますが、そうでない場合は、準備済みステートメントを使用して動的 SQL を生成できます。
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'sum(case when country_id = ''',
country_id,
''' then n_ocurrences end) AS occurrences_',
country_id
)
) INTO @sql
FROM yourtable;
SET @sql = CONCAT('SELECT name, ', @sql, ' ,
sum(n_ocurrences) total_ocurrences
FROM yourtable
GROUP BY name');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
デモで SQL Fiddle を参照してください
両方とも結果が得られます。
| NAME | OCCURRENCES_UK | OCCURRENCES_US | TOTAL_OCURRENCES |
-------------------------------------------------------------
| John | 3 | 4 | 7 |
| Matt | 0 | 5 | 5 |