Solved

oracle query

Posted on 2014-04-30
6
446 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
6 Comments
 
LVL 76

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 76

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now