Oracle query

select to_char(drive_date,'MON') date_label,
to_date(to_char(drive_date,'MON'),'MON') date_order,
       sum(nvl(actual_draw,projection)) as Actual
from blood_drives
 where drive_date between '01-jan-2011' and '31-dec-2011'
   and drive_cancelled is null
   and area_rep_no = 11
 group by area_rep_no, to_date(to_char(drive_date,'MON'),'MON'),
       to_char(drive_date,'MON')
-------------------------------         
select to_char(drive_date,'MON') date_label,
to_date(to_char(drive_date,'MON'),'MON') date_order,
       sum(nvl(actual_draw,projection)) as Actual
from blood_drives
 where drive_date between '01-jan-2012' and '31-dec-2012'
   and drive_cancelled is null
   and area_rep_no = 11
 group by area_rep_no, to_date(to_char(drive_date,'MON'),'MON'),
       to_char(drive_date,'MON')      

Result of 1st query

I have enclosed the results and what is expected in the excel spread sheet that is attached
result-1st-query.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.

Mark GeerlingsDatabase AdministratorCommented:
The results you posted from the queries appear to be from 2014, but your queries are for dates in 2011 and 2012.  So that is one puzzle.

Then, it looks like the output you want is what is called a "matrix" or "cross-tab" report.  Some reporting tools (Crystal Reports, Oracle Reports, etc.) can produce this kind of output, but simple SQL queries cannot.  There may be an advanced SQL keyword that can be used these days to do that, but I haven't used it.

In the "old days", if you wanted to produce this kind of output from a single query without a reporting tool, you had to use "case" or "decode" and hard-code separate sections in your query for each additional column that you wanted in the output.
0
anumosesAuthor Commented:
select to_char(drive_date,'MON') date_label,
to_date(to_char(drive_date,'MON'),'MON') date_order,
       sum(nvl(actual_draw,projection)) as Actual
from blood_drives
 where drive_date between '01-jan-2011' and '31-dec-2011'
   and drive_cancelled is null
   and area_rep_no = 11
 group by area_rep_no, to_date(to_char(drive_date,'MON'),'MON'),
       to_char(drive_date,'MON')

How do I get date_order to be 2011?
0
MikeOM_DBACommented:
You have 11g?
Try this:
WITH Result_1st_Query ( Month, Date_Order, Actual )
     AS (SELECT 'JAN', TO_DATE ( '1/1/2011', 'mm/dd/yyyy' ), 363 FROM DUAL  UNION
         SELECT 'FEB', TO_DATE ( '2/1/2011', 'mm/dd/yyyy' ), 447 FROM DUAL  UNION
         SELECT 'MAR', TO_DATE ( '3/1/2011', 'mm/dd/yyyy' ), 531 FROM DUAL  UNION
         SELECT 'APR', TO_DATE ( '4/1/2011', 'mm/dd/yyyy' ), 610 FROM DUAL  UNION
         SELECT 'MAY', TO_DATE ( '5/1/2011', 'mm/dd/yyyy' ), 552 FROM DUAL  UNION
         SELECT 'JUN', TO_DATE ( '6/1/2011', 'mm/dd/yyyy' ), 695 FROM DUAL  UNION
         SELECT 'JUL', TO_DATE ( '7/1/2011', 'mm/dd/yyyy' ), 455 FROM DUAL  UNION
         SELECT 'AUG', TO_DATE ( '8/1/2011', 'mm/dd/yyyy' ), 478 FROM DUAL  UNION
         SELECT 'SEP', TO_DATE ( '9/1/2011', 'mm/dd/yyyy' ), 407 FROM DUAL  UNION
         SELECT 'OCT', TO_DATE ( '10/1/2011', 'mm/dd/yyyy' ), 623 FROM DUAL UNION
         SELECT 'NOV', TO_DATE ( '11/1/2011', 'mm/dd/yyyy' ), 467 FROM DUAL UNION
         SELECT 'DEC', TO_DATE ( '12/1/2011', 'mm/dd/yyyy' ), 658 FROM DUAL)
   , Result_2nd_Query ( Month, Date_Order, Actual )
     AS (SELECT 'JAN', TO_DATE ( '1/1/2012', 'mm/dd/yyyy' ), 363 FROM DUAL  UNION
         SELECT 'FEB', TO_DATE ( '2/1/2012', 'mm/dd/yyyy' ), 447 FROM DUAL  UNION
         SELECT 'MAR', TO_DATE ( '3/1/2012', 'mm/dd/yyyy' ), 531 FROM DUAL  UNION
         SELECT 'APR', TO_DATE ( '4/1/2012', 'mm/dd/yyyy' ), 610 FROM DUAL  UNION
         SELECT 'MAY', TO_DATE ( '5/1/2012', 'mm/dd/yyyy' ), 552 FROM DUAL  UNION
         SELECT 'JUN', TO_DATE ( '6/1/2012', 'mm/dd/yyyy' ), 695 FROM DUAL  UNION
         SELECT 'JUL', TO_DATE ( '7/1/2012', 'mm/dd/yyyy' ), 455 FROM DUAL  UNION
         SELECT 'AUG', TO_DATE ( '8/1/2012', 'mm/dd/yyyy' ), 478 FROM DUAL  UNION
         SELECT 'SEP', TO_DATE ( '9/1/2012', 'mm/dd/yyyy' ), 407 FROM DUAL  UNION
         SELECT 'OCT', TO_DATE ( '10/1/2012', 'mm/dd/yyyy' ), 623 FROM DUAL UNION
         SELECT 'NOV', TO_DATE ( '11/1/2012', 'mm/dd/yyyy' ), 467 FROM DUAL UNION
         SELECT 'DEC', TO_DATE ( '12/1/2012', 'mm/dd/yyyy' ), 658 FROM DUAL)
-- ------------------------------- --
-- -***- This is the query:  -***- --
SELECT *
  FROM ( SELECT Month, TO_CHAR ( Date_Order, 'YYYY' ) Year, Actual
           FROM Result_1st_Query
          UNION ALL
         SELECT Month, TO_CHAR ( Date_Order, 'YYYY' ), Actual
           FROM Result_2nd_Query )
 PIVOT (SUM ( Actual ) FOR Year IN (2011, 2012))
 ORDER BY TO_DATE ( Month, 'MON' )

Open in new window

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
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:
Sorry I am at 10G
0
Mark GeerlingsDatabase AdministratorCommented:
Do you want the "date_order" to be just the year, or do you also want the numeric month to be included?  Usually, I would get that value with a query something like this:
to_char(drive_date,'YYYY_MM')

That will give you monthly values that sort easily in the correct order.
0
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.