我有一些查询对我来说很好:
Select Name,sum(number_hours)/8)*100
from
T1
where name='PERSON_A'
group by name,booked_date
姓名总是我放在where子句中的同一个人。结果将是:
PERSON_A 100
PERSON_A 140
PERSON_A 120
这是计算每个预订日期工人的日常使用率。 现在我想计算AVERAGE每日利用率((120 + 120 + 100)/ 3 = 120) 但是当我把
Select Name,AVG(sum(number_hours)/8)*100)
FROM
T1
WHERE name='PERSON_A'
group by name,booked_date
我收到错误无效使用群组功能。为什么?如何计算日常利用价值后的平均值。 感谢
答案 0 :(得分:5)
尝试使用子查询求和,并计算外部查询的平均值,如:
SELECT Name, AVG (hsum)
FROM
(
SELECT Name,sum((number_hours)/8)*100 AS hsum
FROM
T1
WHERE name='PERSON_A'
GROUP BY name,booked_date
) t
答案 1 :(得分:2)
我知道你已经接受了答案。但是也可以尝试一下:)没有subquery
。非常快。我为您添加了额外的Count of workdates
列以查看日期。
样本数据表:
ID NAME HOURS WORKDATE
100 j 20 December, 03 2012 00:00:00+0000
200 k 10 December, 03 2012 00:00:00+0000
100 j 10 December, 04 2012 00:00:00+0000
300 l 20 December, 04 2012 00:00:00+0000
100 j 5 December, 05 2012 00:00:00+0000
300 l 15 December, 03 2012 00:00:00+0000
100 j 10 December, 04 2012 00:00:00+0000
400 m 20 December, 03 2012 00:00:00+0000
查询:
SELECT Name, ((sum(hours)/8)*100) AS sum
,count(distinct workdate) workdates, ((sum(hours)/8)*100)/count(
distinct workdate) as avg
FROM
works
WHERE name='j'
GROUP BY name
;
结果:
NAME SUM WORKDATES AVG
j 562.5 3 187.5