Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

GROUP BY LAST 5 WEEK

576637Aug 13 2007 — edited Oct 6 2008
Hi,
I need help to create a sql query to get the avg hours spent per job during the previous 5 weeks, and the avg hours spent per job for the current week for a given period(say for between Jun 2007 and Aug 2007)
I need to get this in one query.

The following fields are required in the output
Week number (YWW format),
Avg. hrs per job for every week
Avg. hrs per job for the previous 5 weeks (This will be 0 for the first 5 weeks)

This is a sample data:
JOB_ID JOB_FINISH_DATE JOB_HOURS
10 15-JUN-07 50
20 21-JUN-07 40
30 10-JUL-07 30
40 15-JUL-07 20
50 18-JUL-07 20
60 21-JUL-07 30
70 01-AUG-07 40
80 10-AUG-07 50
90 12-AUG-07 100

I am using oracle 10g ver 2

I tried using RANGE BETWEEN, BUT NO SUCCESS
avg(nvl(sum(job_info.job_hours),0)) OVER(
ORDER BY TO_NUMBER(to_char(job_info.JOB_FINISH_DATE,'YWW'))
RANGE BETWEEN
TO_NUMBER(to_char(trunc(job_info.JOB_FINISH_DATE-35,'day'),'YWW')) PRECEDING
AND TO_NUMBER(to_char(trunc(job_info.JOB_FINISH_DATE-7,'day')+6,'YWW')) FOLLOWING)
AS Man_hrs_5wk_avg


Appreciate your help.
Thanks.
Anto
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 3 2008
Added on Aug 13 2007
7 comments
14,866 views