MySQL Select - 在数字时合并行

时间:2017-08-20 12:25:12

标签: mysql sql group-by row

我想在一行中显示结果,并按列分隔特定值。

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 |
    -> +----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------+---------------------+

如何将这些行合并为一行?像group by temp_series_id这样的东西并删除空值

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 |
+----------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------+---------------------+

1 个答案:

答案 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;