每天分组

时间:2012-04-11 09:52:47

标签: sql

我想要一个选择结果,我得到每天的结果。我在计算错误,并有以下非工作声明:

SELECT message, time, personid, count(*)
FROM errorlog
WHERE time BETWEEN TO_DATE(todays date - 10 days) AND TO_DATE(todays date) AND SUBSTR(message,0,3) = 'ERR'
GROUP BY DAY(time), personid, message
ORDER BY 4

当我运行它时,它说“DAY”:无效的标识符。

3 个答案:

答案 0 :(得分:1)

它应该是GROUP BY之后的列名。

TRY:

SELECT message, DAY(time) dayTime, personid, count(*)
FROM errorlog
WHERE time BETWEEN TO_DATE(todays date - 10 days) AND TO_DATE(todays date) AND SUBSTR(message,0,3) = 'ERR'
GROUP BY dayTime, personid, message
ORDER BY 4

答案 1 :(得分:0)

GROUP BY中的所有项目也必须位于SELECT

如果您按天分组,并显示time(我怀疑是日期时间数据类型),并且您在哪一天有2条记录,您的结果集将如何显示这些两个不同的时间?它无法做到。

以下查询使用CONVERT(VARCHAR(10), time, 103)中的SELECTGROUP BY,表示结果集中将显示dd/MM/yyyy格式,并且您的记录将被分组通过这个。

SELECT message, CONVERT(VARCHAR(10), time, 103), personid, count(*)
FROM errorlog
WHERE time BETWEEN TO_DATE(todays date - 10 days) AND TO_DATE(todays date) AND SUBSTR(message,0,3) = 'ERR'
GROUP BY CONVERT(VARCHAR(10), time, 103), personid, message
ORDER BY 4

答案 2 :(得分:-1)

SQL SERVER?

纠正这个:

SELECT message, DAY(time) AS [DAY], personid, count(*)