MySQL格式为行作为列

时间:2018-03-13 19:59:27

标签: mysql sql

我正在开发一个更新的内部网站,它将从sql server中提取数据。数据将自动从VAX系统插入数据库。原始数据库如下所示:

mysql> SELECT * FROM userdb.clientdata WHERE datadate = '2016-09-23' AND header IN (1,3,6,9,212,2048);
+--------+--------------+-------------+----------+--------+-----------+-------------------------+---------------+---------------+
| id     | clientnumber | plantnumber | datahour | header | datavalue | datadate                | projectnumber | dec_precision |
+--------+--------------+-------------+----------+--------+-----------+-------------------------+---------------+---------------+
|  28673 |         NULL |           2 |        1 |      1 | -5865.97  | 2016-09-23 00:00:00.000 |          NULL |             1 |
|  28675 |         NULL |           2 |        1 |      3 | 78.368    | 2016-09-23 00:00:00.000 |          NULL |             1 |
|  28678 |         NULL |           2 |        1 |      6 | -5865.97  | 2016-09-23 00:00:00.000 |          NULL |             1 |
|  28681 |         NULL |           2 |        1 |      9 | -5865.97  | 2016-09-23 00:00:00.000 |          NULL |             1 |
|  28884 |         NULL |           2 |        1 |    212 | 0         | 2016-09-23 00:00:00.000 |          NULL |             0 |
|  30720 |         NULL |           2 |        1 |   2048 | 0         | 2016-09-23 00:00:00.000 |          NULL |             0 |
|  30721 |         NULL |           2 |        2 |      1 | -5865.97  | 2016-09-23 00:00:00.000 |          NULL |             1 |
|  30723 |         NULL |           2 |        2 |      3 | 77.342    | 2016-09-23 00:00:00.000 |          NULL |             1 |
|  30726 |         NULL |           2 |        2 |      6 | -5865.97  | 2016-09-23 00:00:00.000 |          NULL |             1 |
|  30729 |         NULL |           2 |        2 |      9 | -5865.97  | 2016-09-23 00:00:00.000 |          NULL |             1 |
|  30932 |         NULL |           2 |        2 |    212 | 0         | 2016-09-23 00:00:00.000 |          NULL |             0 |
|  32768 |         NULL |           2 |        2 |   2048 | 0         | 2016-09-23 00:00:00.000 |          NULL |             0 |

我需要能够对其进行格式化,因此结果如下所示:

+-------------------------+----------+---------+---------+---------+---------+-----------+------------+
| datadate                | datahour | header1 | header3 | header6 | header9 | header212 | header2048 |
+-------------------------+----------+---------+---------+---------+---------+-----------+------------+
| 2016-09-23 00:00:00.000 |        1 | -5865.97|  78.368 | -5865.97| -5865.97|         0 |          0 |
| 2016-09-23 00:00:00.000 |        2 | -5865.97|  77.342 | -5865.97| -5865.97|         0 |          0 |

任何帮助都将不胜感激。

MySQL 5.7.21 for Linux

编辑:该网站将使用php,但我正在寻找实际的MySQL查询来提供列出的结果,以便我可以将其存储在另一个表中。

3 个答案:

答案 0 :(得分:1)

您应该能够按datahour进行分组并自定义标题行,如下所示:

SELECT 
    datadate, 
    datahour, 
    SUM(IF(header = '1', datavalue, 0)) as header1,
    SUM(IF(header = '3', datavalue, 0)) as header3,
    SUM(IF(header = '6', datavalue, 0)) as header6,
    SUM(IF(header = '9', datavalue, 0)) as header9,
    SUM(IF(header = '212', datavalue, 0)) as header212,
    SUM(IF(header = '2048', datavalue, 0)) as header2048
FROM userdb.clientdata
WHERE datadate = '2016-09-23' AND header IN (1,3,6,9,212,2048)
GROUP BY datahour;

此解决方案对您的数据做出重要的假设:在一个“datahour”中没有特定标题的重复条目

答案 1 :(得分:1)

您可以使用条件聚合在MySQL中进行透视:

SELECT datadate, datahour, 
       MAX(CASE WHEN header = 1 THEN datavalue END) as header1,
       MAX(CASE WHEN header = 3 THEN datavalue END) as header3,
       MAX(CASE WHEN header = 6 THEN datavalue END) as header6,
       MAX(CASE WHEN header = 9 THEN datavalue END) as header9,
       MAX(CASE WHEN header = 212 THEN datavalue END) as header212,
       MAX(CASE WHEN header = 2048 THEN datavalue END) as header2048
FROM userdb.clientdata
WHERE datadate = '2016-09-23' AND header IN (1, 3, 6, 9, 212, 2048)
GROUP BY datadate, datahour
ORDER BY datadate, datahour;

答案 2 :(得分:0)

这是一种方法。我在您的问题中使用ucd代表userdb.clientdata

SELECT DATE_FORMAT(a.datadate, '%Y-%m-%d %T.%s') datadate, a.datahour,
(SELECT datavalue FROM ucd WHERE a.datahour = datahour and header = 1)  header1,
(SELECT datavalue FROM ucd WHERE a.datahour = datahour and header = 3)  header3,
(SELECT datavalue FROM ucd WHERE a.datahour = datahour and header = 6)  header6,
(SELECT datavalue FROM ucd WHERE a.datahour = datahour and header = 9)  header9,
(SELECT datavalue FROM ucd WHERE a.datahour = datahour and header = 212)  header212,
(SELECT datavalue FROM ucd WHERE a.datahour = datahour and header = 2048)  header2048
FROM (SELECT DISTINCT datadate, datahour FROM ucd) a
WHERE a.datadate = '2016-09-23 00:00:00.000'

我会尝试逐步解释,如果有错误,请纠正我。

  1. 首先在表格中选择不同的datahour
  2. 然后在datavalue等于某个值(1,3,6等)时找到header并填写相应的列。
  3. <强> RESULT

    datadate                datahour    header1   header3   header6    header9  header212   header2048
    2016-09-23 00:00:00.00     1       -5865.97   78.368    -5865.97   -5865.97 0           0
    2016-09-23 00:00:00.00     2       -5865.97   77.342    -5865.97   -5865.97 0           0
    

    请在SQL Fiddle

    中找到代码