Solved

oracle query

Posted on 2014-04-30
6
449 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.

Question has a verified solution.

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

Suggested Solutions

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

777 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