我有这样的桌子:
Year Month Type
2013 4 31
2013 3 31
2014 5 40
2014 6 41
2015 5 31
2015 7 40
2013 4 31
2013 3 31
2014 5 40
2014 6 41
2015 5 31
2015 7 40
2013 4 31
2013 3 31
我想计算每个组合的出现次数。所以输出应该是这样的:
Year Month Type Count_of_appearance
2013 3 31 3
2013 4 31 3
etc..
我一直在使用类似的东西:
SELECT Year,
Month,
Type,
(SELECT COUNT (*)
FROM myTable
WHERE (...im lost in defining a condition in order to make this work...)
AS Count_of_appearance
FROM employee;
在定义功能性WHERE子句时我迷路了。
问题是我有很多这样的字段和很多唯一的值。表很大8Gb。
答案 0 :(得分:1)
您正在寻找GROUP BY
:
SELECT Year, Month, Type, COUNT(*) AS Count_of_appearance
FROM employee
GROUP BY Year, Month, Type
ORDER BY Year, Month, Type;
为确保结果顺序正确,还应包括ORDER BY
。