Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

oracle query

Posted on 2014-04-30
6
Medium Priority
?
459 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 78

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 78

Accepted Solution

by:
slightwv (䄆 Netminder) earned 1000 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 49

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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

963 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