Solved

oracle query

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting a row into a column 2 60
execute immediate plsql block 5 57
PL/SQL Display based on value 4 39
Select and Insert Query running slow 4 45
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

765 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