2017-08-20 11 views
1

結果を1行に表示し、特定の値の列で区切りたい。MySQL Select - 数値のときに行をマージする

mysql> +----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------+---------------------+ 
    -> | temp_series_id | CO | DP | BO | DZ | KT | KR | DR | GZ | GP | PD | CD | date    | 
    -> +----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------+---------------------+ 
    -> |   272138 | 21.12 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2017-08-20 14:06:46 | 
    -> |   272138 | NULL | 20.06 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2017-08-20 14:06:46 | 
    -> |   272138 | NULL | NULL | 18.69 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2017-08-20 14:06:46 | 
    -> |   272138 | NULL | NULL | NULL | 21.81 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2017-08-20 14:06:46 | 
    -> |   272138 | NULL | NULL | NULL | NULL | 22.06 | NULL | NULL | NULL | NULL | NULL | NULL | 2017-08-20 14:06:46 | 
    -> |   272138 | NULL | NULL | NULL | NULL | NULL | 22.44 | NULL | NULL | NULL | NULL | NULL | 2017-08-20 14:06:46 | 
    -> |   272138 | NULL | NULL | NULL | NULL | NULL | NULL | 14.69 | NULL | NULL | NULL | NULL | 2017-08-20 14:06:46 | 
    -> |   272138 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 24.69 | NULL | NULL | NULL | 2017-08-20 14:06:46 | 
    -> |   272138 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 22.75 | NULL | NULL | 2017-08-20 14:06:46 | 
    -> |   272138 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 19.94 | NULL | 2017-08-20 14:06:46 | 
    -> |   272138 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 24.5 | 2017-08-20 14:06:46 | 
    -> +----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------+---------------------+ 

どのようにこの行を1つにマージできますか? group by temp_series_idと削除NULL値

SELECT 
    SQL_NO_CACHE 
    temp_series_id, 
    CASE WHEN temp_details_id = 1 THEN value END AS 'CO', 
    ... 
    CASE WHEN temp_details_id = 11 THEN value END AS 'CD', 
    date 
FROM (SELECT * 
     FROM temp_series 
     ORDER BY date DESC 
     LIMIT 1) 
    series INNER JOIN temp_object objects ON series.id = objects.temp_series_id 
    INNER JOIN temp_details details ON details.id = objects.temp_details_id; 

ような何か私は

+----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------+---------------------+ 
| temp_series_id | CO | DP | BO | DZ | KT | KR | DR | GZ | GP | PD | CD | date    | 
+----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------+---------------------+ 
|   272138 | 21.12 | 20.06 | 18.69 | 21.81 | 22.06 | 22.44 | 14.69 | 24.69 | 22.75 | 19.94 | 24.5 | 2017-08-20 14:06:46 | 
+----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------+---------------------+ 
+0

は、 'ピボットテーブル' –

答えて

1

使用集約したいと思った結果と見ることができます下:

SELECT SQL_NO_CACHE 
     temp_series_id, 
     MAX(CASE WHEN temp_details_id = 1 THEN value END) AS CO, 
     ... 
     MAX(CASE WHEN temp_details_id = 11 THEN value END) AS CD, 
     date 
FROM (SELECT * 
     FROM temp_series 
     ORDER BY date DESC 
     LIMIT 1 
    ) series INNER JOIN 
    temp_object objects 
    ON series.id = objects.temp_series_id INNER JOIN 
    temp_details details 
    ON details.id = objects.temp_details_id 
GROUP BY temp_series_id, date; 
+1

MAXについて読むことをお勧めし()魔法の作品、ありがとう –

関連する問題