选择一个或具有一个不同值列SQL的Merger Multiple行

时间:2015-06-25 04:09:50

标签: mysql sql

请帮助我,我怎样才能获得重复数据之一: 表就像:

样本表:

  datetime          |service|error_code |trx   |request
2015-06-22 23:05:11 |808    |00000000   |320456|160228
2015-06-22 23:05:11 |808    |00008008   |0     |2
2015-06-23 23:05:08 |808    |00000000   |324426|162213
2015-06-23 23:05:08 |808    |00008001   |0     |2
2015-06-23 23:05:08 |808    |00008008   |0     |2
2015-06-24 23:05:09 |808    |00000000   |333402|166701
2015-06-24 23:05:09 |808    |00008008   |0     |2

这是我的查询

  SELECT DATE(datetime) as datex, SUBSTR(datetime,12,8) as timex,
 service,error_code, MAX(trx) as trx, MAX(request) as req
    FROM (SELECT * FROM tmenu_trx WHERE error_code not in ('00000000'))x
        GROUP BY 1, 2, 3, 4

我查询的结果是

date       | time    |service| error_code|trx|req
2015-06-22 |23:05:11 |  808  |00008008   |0  |2
2015-06-23 |23:05:08 |  808  |00008001   |0  |2
2015-06-23 |23:05:08 |  808  |00008008   |0  |2
2015-06-24 |23:05:09 |  808  |00008008   |0  |2

但我希望数据如下:

    date       | time    |service| error_code|trx|req
    2015-06-22 |23:05:11 |  808  |00008008   |0  |2
    2015-06-23 |23:05:08 |  808  |00008001   |0  |2
    2015-06-24 |23:05:09 |  808  |00008008   |0  |2

或:

    date       | time    |service| error_code|trx|req
    2015-06-22 |23:05:11 |  808  |00008008   |0  |2
    2015-06-23 |23:05:08 |  808  |00008008   |0  |2
    2015-06-24 |23:05:09 |  808  |00008008   |0  |2

或:

date       | time    |service| error_code       |trx |req
2015-06-22 |23:05:11 |  808  |00008008          |0   |2
2015-06-23 |23:05:08 |  808  |00008001:00008008 |0   |2
2015-06-24 |23:05:09 |  808  |00008008          |0   |2

1 个答案:

答案 0 :(得分:0)

试试这个

for first one

SELECT DATE(datetime) as datex, SUBSTR(datetime,12,8) as timex,
service,error_code, MAX(trx) as trx, MAX(request) as req
FROM table1 WHERE error_code not in ('00000000')
GROUP BY DATE(datetime)

for second one

SELECT DATE(datetime) as datex, SUBSTR(datetime,12,8) as timex,
service,max(error_code), MAX(trx) as trx, MAX(request) as req
FROM table1 
GROUP BY DATE(datetime)

第三个

SELECT DATE(datetime) as datex, SUBSTR(datetime,12,8) as timex,
service,GROUP_CONCAT(error_code SEPARATOR ':'), MAX(trx) as trx,MAX(request) as req
FROM table1 WHERE error_code not in ('00000000')
GROUP BY DATE(datetime)