选择计数&按sysdate计算除法

时间:2015-05-23 09:24:03

标签: sql oracle

我有这样的剧本。它返回员工完成的请求的值。我喜欢将第二个表的结果除以“(sysdate-entrydate)”的天数,但它返回的错误不是按功能分组。我喜欢在结果中有3个表。我想到了子查询,但我想嵌套的查询不起作用(计数除以sysdate-entrydate)。有任何想法吗?提前谢谢。

select username, count(username)
from requesttool.request_queue
inner join requesttool.request_detail 
on requesttool.request_detail.request_id = requesttool.request_queue.request_id

inner join TASKTOOL.task_source_type_codes
on requesttool.request_queue.request_source_id = tasktool.task_source_type_codes.task_source_id

inner join admin.users 
on coalesce(requesttool.request_queue.REASSIGNED_ANALYST_ID,requesttool.request_queue.ASSIGNED_ANALYST_ID) = admin.users.userid

where ATTRIBUTE_ID = 259

and STATUS_ID in ('3','4')
group by username

第二个脚本(不工作):

select username, count (username), count(username)/(sysdate-entrydate)
from requesttool.request_queue
inner join requesttool.request_detail 
on requesttool.request_detail.request_id = requesttool.request_queue.request_id

 inner join TASKTOOL.task_source_type_codes
on requesttool.request_queue.request_source_id = tasktool.task_source_type_codes.task_source_id

inner join admin.users 
on coalesce(requesttool.request_queue.REASSIGNED_ANALYST_ID,requesttool.request_queue.ASSIGNED_ANALYST_ID) = admin.users.userid

where ATTRIBUTE_ID = 259

and STATUS_ID in ('3','4')

group by username
order by count(username) desc

示例:

Mr A has 10,000 requests done joined 6 years ago (2190 days)
Mr B has 1,000 requests done but joined 1 year ago

Username // Count (username) // Average per day
Mr A // 10,000 // 4.56 (10000 / 2190 = 4.65 requests a day)
Mr B // 1,000 // 2.73 (1000 / 365 = 2.73 requests a day)

解决方案:     按用户名分组,entrydate

1 个答案:

答案 0 :(得分:3)

从您的帖子中,我猜entrydate是用户的属性。因此,对于一个username,只有一个entrydate。但Oracle不知道这一点,所以它给出了一个错误,它无法判断使用哪个entrydate

您可以通过(username, entrydate)分组来解决此问题。这让Oracle知道每个组只有一个entrydate

group by username, entrydate