[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Oracle query

Posted on 2014-07-11
6
Medium Priority
?
389 Views
Last Modified: 2014-07-11
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
0
Comment
Question by:anumoses
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 40190883
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
 
LVL 6

Author Comment

by:anumoses
ID: 40190921
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
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 1000 total points
ID: 40191017
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:anumoses
ID: 40191032
Sorry I am at 10G
0
 
LVL 35

Assisted Solution

by:Mark Geerlings
Mark Geerlings earned 1000 total points
ID: 40191094
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
 
LVL 6

Author Closing Comment

by:anumoses
ID: 40191196
thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question