我想知道是否有人可以帮助我。以下是我的数据样本集:
FirstName LastName Department Comment DateWorked
Bob Smith Sleeves 2017-01-01
Jim Bo Sleeves 2017-01-01
Janet Moore Lids No Show 2017-01-01
Jon Bob Lids 2017-01-01
Bob Smith Mugs 2017-01-02
Bob Smith Sleeves 2017-01-03
Jim Bo Sleeves 2017-01-03
Janet Moore Lids 2017-01-03
Jon Bob Lids 2017-01-03
应该返回这样的内容:
DateWorked Department HeadCount
2017-01-01 Sleeves 2
2017-01-01 Lids 2
2017-01-02 Mugs 1
2017-01-03 Sleeves 2
2017-01-03 Lids 2
到目前为止,我已尝试过一些事情。
这就是我想要的但它不起作用
SELECT Count(Distinct(FirstName, LastName, Department, Scheduled), Notes) FROM Employees
Where Scheduled < 20171231
and Scheduled > 20170101
Group by Scheduled, Department, FirstName, LastName, Department, Comment
这只是给了我一个数字。
select count(*)
from
(select count(*) CT
from Employees
group by Scheduled, Department) TD
这个错误了。
SELECT COUNT(DISTINCT FirstName, LastName, Department) FROM Employees
有人可以帮忙吗? 感谢
答案 0 :(得分:1)
您似乎想要:
SELECT DateWorked, Department, COUNT(*) as HeadCount
FROM Employees
WHERE Scheduled < 20171231 AND Scheduled > 20170101
GROUP BY DateWorked, Department
ORDER BY DateWorked, Department;
上面的内容会保留您在查询中的日期比较,尽管它们看起来不对。
我建议将日期比较写为:
SELECT DateWorked, Department, COUNT(*) as HeadCount
FROM Employees
WHERE Scheduled >= '2017-01-01' AND Scheduled < '2017-12-31'
GROUP BY DateWorked, Department
ORDER BY DateWorked, Department;
这会修复日期比较,使其与您想要的结果更加一致。
您没有指定数据库。 YYYY-MM-DD是ISO标准日期格式,适用于大多数数据库。
答案 1 :(得分:0)
SELECT DateWorked, Department, COUNT(*) AS HeadCount
FROM Employees
WHERE Scheduled < 20171231 AND Scheduled > 20170101
GROUP BY Department, DateWorked