我有以下SQLFiddle,我可以按时间,计数和名称对结果进行分组,但我也想在同一行中返回error_strings。
有什么建议吗? Group concat并没有真正做我需要的。
我希望输出看起来像:
NAME | TIME | TODAYSCOUNT | ERRORS
------------------------------------
A 1111 2000 error1,erro2
B 1111 20 error1,erro2
C 1111 10 error1,erro2
D 1111 3 error1,erro2
答案 0 :(得分:3)
下面怎么样: -
http://sqlfiddle.com/#!2/4c5e4/20
SELECT name,time,count(*) as todaycount, group_concat(error_str) as errors
FROM test
GROUP BY Name,time
您可以更改为
group_concat(distinct error_str)
表示不同的错误字符串。
答案 1 :(得分:1)
看看这个:
http://sqlfiddle.com/#!2/d6929/2
我向前迈了一步,因为看起来你可能会把它拿走这么远:
SELECT Name, FROM_UNIXTIME('%m-%d-%Y', SUM(TodaysDate)),FROM_UNIXTIME('%m-%d-%Y',SUM(YesterdaysDate)), sum(TodaysCount),Sum(YesterdaysCount),CONCAT(String),CONCAT(String2)
FROM (
SELECT Name,Time as TodaysDate,0 as YesterdaysDate,COUNT(*) as TodaysCount, 0 as YesterdaysCount,GROUP_CONCAT(error_str) as String, '' as String2
FROM Test
WHERE Time = 3111
GROUP BY Name
UNION ALL
SELECT Name,0 as TodaysDate,Time as YesterdaysDate,0 as TodaysCount, COUNT(*) as YesterdaysCount, '' as String, GROUP_CONCAT(error_str) as String2
FROM Test
WHERE Time = 1111
GROUP BY Name
) as t
GROUP BY Name
以上使用过的工会