2

我有以下SQLFiddle,我可以按时间、计数和名称对结果进行分组,但我也想在同一行中返回 error_strings。

有什么建议么?组 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
4

2 回答 2

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)

对于不同的错误字符串。

于 2012-06-28T02:06:29.927 回答
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

上面使用的工会

于 2012-06-28T01:57:03.203 回答