Solved

oracle query

Posted on 2014-04-30
6
452 Views
Last Modified: 2014-05-01
http://www.experts-exchange.com/Database/Oracle/Q_28423193.html

select drive_date,TO_CHAR(DRIVE_DATE,'Day') day_of_week,
    count(case when COACH_DRIVE = 'C' then 'C' end ) coach,
    count(case when COACH_DRIVE = 'M' then 'M' end ) mini, count(*) totals
from blood_drives
where drive_date between '01-jun-2014' and '31-aug-2014'
and coach_drive in ('C','M')
and drive_cancelled is null
group by drive_date,TO_CHAR(DRIVE_DATE,'Day')
order by drive_date



Users keep changing what they need. So entering another question. Will close the previous one. That worked. Now they need

They need ALL the Sunday’s totals added together, all the Monday’s, etc.
coach and mini added together.
0
Comment
Question by:anumoses
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40032675
>>They need ALL the Sunday’s totals added together, all the Monday’s, etc.
coach and mini added together.

What is different here than your other question?

What does the new expected results look like?
0
 
LVL 6

Author Comment

by:anumoses
ID: 40032707
Attaching the file
Drives-by-Day-of-the-Week.xls
0
 
LVL 16

Expert Comment

by:Wasim Akram Shaik
ID: 40032742
==>They need ALL the Sunday’s totals added together, all the Monday’s, etc.
coach and mini added together.

Are they asking roll up total for day wise, try something like this

select drive_date,TO_CHAR(DRIVE_DATE,'Day') day_of_week,
    count(case when COACH_DRIVE = 'C' then 'C' end ) coach,
    count(case when COACH_DRIVE = 'M' then 'M' end ) mini, count(*) totals
from blood_drives
where drive_date between '01-jun-2014' and '31-aug-2014'
and coach_drive in ('C','M')
and drive_cancelled is null
group by drive_date,rollup(TO_CHAR(DRIVE_DATE,'Day'))
order by drive_date
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 250 total points
ID: 40032757
rollup might be the way to go but I'm not that familiar with it or how to get it into your desired results.

Here is what I came up with.  I think it matches your desired results.

select drive_date, day, coach_count, mini_count, day_count, case when rn=max_rn then day_total end day_total
from
(
select drive_date, day, coach_count, mini_count, day_count, rn,
max(rn) over(partition by day) max_rn,
	sum(day_count) over(partition by day) day_total
from (
select drive_date, TO_CHAR(DRIVE_DATE,'DAY') day,
    count(case when COACH_DRIVE = 'C' then 'C' end) coach_count, 
    count(case when COACH_DRIVE = 'M' then 'M' end) mini_count,
    count(*) day_count,
    row_number() over(partition by TO_CHAR(DRIVE_DATE,'DAY') order by TO_CHAR(DRIVE_DATE,'DAY') ) rn
from blood drives
where drive_date between '01-jun-2014' and '31-aug-2014'
and coach_drive in ('C','M')
and drive_cancelled is nullgroup by drive_date, TO_CHAR(DRIVE_DATE,'DAY') 
)
order by 2
)
/

Open in new window

0
 
LVL 6

Author Closing Comment

by:anumoses
ID: 40032898
perfect
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40034593
>>"rollup might be the way to go"

doubt it, "group by rollup" will introduce rows for the day totals, not an extra column
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle SQL - Query help 7 80
Oracle DBLINKS From 11g to 8i 3 67
Oracle function to insert records? 15 65
Create file system directory from Oracle 10g 4 33
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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.

730 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