需要以下查询的帮助
此时显示按年度季度分组的“工作时间”的用户列表。 我还希望按工作天数求和 - 问题是可以有不止一个 每天排,所以“onsitedate”的总和是不正确的。 我不能按“日”分组,因为结果需要按年度季度显示。
SQL
SELECT
username,
DatePart ('q', [onsitedate]) AS [qtr],
SUM(onsitehours),
SUM(onsitedate)
FROM
clocktable
WHERE onsitedate BETWEEN #2013-01-01# AND #2013-12-31#
GROUP BY username, DatePart('q',[onsitedate])
ORDER BY username, DatePart('q',[onsitedate])
如果需要更多信息和TQ
,请告诉我更新
如上所述,我应该在sitedate line(错字)上使用COUNT
SELECT
username,
DatePart ('q', [onsitedate]) AS [qtr],
SUM(onsitehours),
**COUNT(onsitedate)**
FROM
但是如果同一日期存在两条记录,那么我希望计数为1
示例数据如下:
username onsitedate onsitehours
-----------------------------------------------
bob 01/09/2013 10
bob 01/09/2013 2
fred 01/09/2013 12
jim 01/09/2013 10
jim 02/09/2013 5
要求rs
username qtr onsitedate_count onsitehours_sum
---------------------------------------------------------
bob 3 1 12
fred 3 1 12
jim 3 2 15
如你所见 - 尽管在01/09/2013有两条bob记录,onsitedate_count仍为1 因为他们是同一天
也试过以下,以便将日期分组,但没有快乐
GROUP BY tech_name, DatePart('d',[on_site_date]), DatePart('q',[on_site_date])
答案 0 :(得分:2)
首先创建一个查询,以便对onsitehours
和username
的每个组合onsitedate
求和。
SELECT
username,
DatePart ('q', [onsitedate]) AS [qtr],
onsitedate,
SUM(onsitehours) AS SumOfonsitehours
FROM
clocktable
WHERE onsitedate BETWEEN #2013-01-01# AND #2013-12-31#
GROUP BY username, DatePart('q',[onsitedate]), onsidedate;
然后将其用作子查询并计算日期,并为username
和qtr
的每个组合再次计算小时数。
SELECT
sub.username,
sub.qtr,
Count(sub.onsitedate) AS onsitedate_count,
Sum(sub.SumOfonsitehours) AS onsitehours_sum
FROM
(
SELECT
username,
DatePart ('q', [onsitedate]) AS [qtr],
onsitedate,
SUM(onsitehours) AS SumOfonsitehours
FROM
clocktable
WHERE onsitedate BETWEEN #2013-01-01# AND #2013-12-31#
GROUP BY username, DatePart('q',[onsitedate]), onsitedate
) AS sub
GROUP BY sub.username, sub.qtr
ORDER BY sub.username, sub.qtr;
使用您的clocktable
示例数据,该查询将在Access 2007中返回您请求的输出。
答案 1 :(得分:0)
只是不要SUM(现场日期)。如果我理解你的问题
SELECT
a.username,
DatePart ('q', a.[onsitedate]) AS [qtr],
COUNT(b.[onsitedate]) onsitedate_count,
SUM(a.onsitehours)
FROM clocktable a
join (SELECT username, [onsitedate])
FROM clocktable GROUP BY username,[onsitedate]) b
on a.username = b.username
WHERE a.onsitedate BETWEEN #2013-01-01# AND #2013-12-31#
GROUP BY a.username, DatePart('q',a.[onsitedate]), b.onsitedate_count
ORDER BY a.username, DatePart('q',a.[onsitedate])