oracle query with max and group by clause

Enclosing a sample table script

Query to run

select max(day_of_week) as day_of_week, max(month) as month,order_by,
case when rep_id = 2  then goals end as Jen,
case when rep_id = 11  then  goals end as Sheri
from wkly_proposed_goal_improve
group by rep_id,goals,order_by
order by order_by

output is enclosed as spreadsheet.

Required or expected output is enclosed

I also wanted the query to give me data from the present month that we are running the report.  For now from JUL onwards.

I tried using where MONTH = to_char(sysdate,'MON') that is for jul
for going upto DEC  I could not get when I used >=
table-script.txt
query-output.xls
expected.xls
LVL 6
anumosesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sdstuberCommented:
SELECT MAX(day_of_week) AS day_of_week,
         MAX(month) AS month,
         order_by,
         MAX(CASE WHEN rep_id = 2 THEN goals END) AS jen,
         MAX(CASE WHEN rep_id = 11 THEN goals END) AS sheri
    FROM wkly_proposed_goal_improve
GROUP BY order_by
ORDER BY order_by
0
anumosesAuthor Commented:
Thanks for the solution

I also wanted the query to give me data from the present month that we are running the report.  For now from JUL onwards.
0
sdstuberCommented:
that's not what your expected results showed.

is month meaningful as a date criteria?

What about January of next year?  That's in the future, but January of last year is in the past.  How will comparing JAN to JUL help determine which is which?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

anumosesAuthor Commented:
I have the year in the table as of now it is for 2014. The user adds data to the table with year and corresponding goals

The below is the query that runs as a dbms_job every sunday night

 select distinct trunc(sysdate+1) as day_of_week,
                 YEAR,
                         a.REP_ID,
                         GOALS,
                         a. MONTH,
                         ORDER_BY,
                         GOALS*120/100 as booking_target,
                 booked_now, ( booked_now- (GOALS*120/100)) as booking_surplus_deficit
from  proposed_goal a , proposed_annual_goal b
 where a.rep_id = b.rep_id
   and a.month = b.month
   and booked_now is not null
 order by rep_id,order_by

So I will pull the report by adding the year.
0
sdstuberCommented:
SELECT MAX(day_of_week) AS day_of_week,
       MAX(month) AS month,
       order_by,
       MAX(CASE WHEN rep_id = 2 THEN goals END) AS jen,
       MAX(CASE WHEN rep_id = 11 THEN goals END) AS sheri
  FROM wkly_proposed_goal_improve
 WHERE year = EXTRACT(YEAR FROM SYSDATE)
   AND TO_DATE(year || month, 'yyyy-mm') >= TRUNC(SYSDATE, 'mm')
GROUP BY order_by
ORDER BY order_by
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anumosesAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.