SQL distinct或group by

时间:2010-10-18 13:31:38

标签: sql group-by distinct

我的大脑不起作用..你能不能帮帮我吧?

找到了date, analyseid, groupid

的表格

我想得到独特的groupid ..也许是对同一个groupid的计数。此外,如果存在具有相同groupid的行,则日期也相同。

SELECT     date, analyseid,  COUNT(*) AS 'amount', groupid
FROM         myTable
GROUP BY groupid
ORDER BY date DESC

提前致谢

1 个答案:

答案 0 :(得分:2)

您必须GROUP BY所有列SELECT(聚合函数除外):

SELECT     date, analyseid,  COUNT(*) AS 'amount', groupid
FROM         myTable
GROUP BY date, analyseid,  groupid
ORDER BY date DESC

如果您只想要groupid和相应的计数,请从date, analyseidSELECT中删除GROUP BY

SELECT     COUNT(*) AS 'amount', groupid
FROM         myTable
GROUP BY groupid