Solved

oracle query

Posted on 2014-01-20
5
326 Views
Last Modified: 2014-01-20
select c.location_name center,
       b.location_id,
         a.coll_date,
       sum(decode(a.procedure_code, 'PL', 1, 0)) jumbo,
       sum(decode(a.procedure_code, 'CO', 1, 'CX', 1, 'RS', 1, 0)) concurrent
          from donations_don a,
               drives_don b,
                     locations_don c
         where a.drive_id = b.drive_id
           and a.coll_date between '01-JAN-2014' and '02-JAN-2014'
               and b.location_id = c.location_id
           and c.location_type = 'FIX'
           and a.donation_type = 'AL'
           and a.procedure_code in ('PL','CO','CX','RS')
           and a.unit_id is not null
               and (c.term_date is null or c.term_date > '01-JAN-2014')
           and      b.location_id =  'LOCN100008'
           and not exists (select 'x'
                             from donation_attributes_don da
                            where da.transaction_id = a.transaction_id
                              and da.attrib_code = 'VENI'
                              and da.attrib_value in ('04', '05'))
         group by c.location_name, b.location_id, a.coll_date
union
select distinct c.location_name center,
          b.location_id,
          a.coll_date,
          0,
          0
   from donations_don a,
           drives_don b,
           locations_don c            
where a.drive_id = b.drive_id
           and a.coll_date between '01-JAN-2014' and '02-JAN-2014'
               and b.location_id = c.location_id
           and c.location_type = 'FIX'
           and a.donation_type = 'AL'
           and a.procedure_code in ('PL','CO','CX','RS')
           and a.unit_id is not null
               and (c.term_date is null or c.term_date > '01-JAN-2014')
           and b.location_id = 'LOCN100008'
           and not exists (select 'x'
                             from donation_attributes_don da
                            where da.transaction_id = a.transaction_id
                              and da.attrib_code = 'VENI'
                              and da.attrib_value in ('04', '05'))
         group by c.location_name, b.location_id, a.coll_date
 order by 1, 2, 3      


-------------------------

CENTER,LOCATION_ID,COLL_DATE,JUMBO,CONCURRENT

AURORA CENTER,LOCN100008,1/2/2014,0,0

AURORA CENTER,LOCN100008,1/2/2014,0,3


Required result

CENTER,LOCATION_ID,COLL_DATE,JUMBO,CONCURRENT

AURORA CENTER,LOCN100008,1/2/2014,0,3
0
Comment
Question by:anumoses
  • 2
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39794589
you may want to read up this article:
http://www.experts-exchange.com/Database/Miscellaneous/A_3203-DISTINCT-vs-GROUP-BY-and-why-does-it-not-work-for-my-query.html

reading the query does not make it obvious to see where the issue could come from, except that I don't see the need of the 2nd part (everything after the UNION ...)
0
 
LVL 6

Author Comment

by:anumoses
ID: 39794596
After the union ends

------------------------- Data displayed

CENTER,LOCATION_ID,COLL_DATE,JUMBO,CONCURRENT

AURORA CENTER,LOCN100008,1/2/2014,0,0

AURORA CENTER,LOCN100008,1/2/2014,0,3


Required result

CENTER,LOCATION_ID,COLL_DATE,JUMBO,CONCURRENT

AURORA CENTER,LOCN100008,1/2/2014,0,3
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39794600
why would expect there to be only one row?

You have the same query twice, except the second one forces zeroes for the last two values.

So, the only way the UNION could remove the duplicates is if the first query returned zeroes from the sums

Simply remove the union and only run the first part of the query


try this...


select c.location_name center,
       b.location_id,
         a.coll_date,
       sum(decode(a.procedure_code, 'PL', 1, 0)) jumbo,
       sum(decode(a.procedure_code, 'CO', 1, 'CX', 1, 'RS', 1, 0)) concurrent
          from donations_don a,
               drives_don b,
                     locations_don c
         where a.drive_id = b.drive_id
           and a.coll_date between '01-JAN-2014' and '02-JAN-2014'
               and b.location_id = c.location_id
           and c.location_type = 'FIX'
           and a.donation_type = 'AL'
           and a.procedure_code in ('PL','CO','CX','RS')
           and a.unit_id is not null
               and (c.term_date is null or c.term_date > '01-JAN-2014')
           and      b.location_id =  'LOCN100008'
           and not exists (select 'x'
                             from donation_attributes_don da
                            where da.transaction_id = a.transaction_id
                              and da.attrib_code = 'VENI'
                              and da.attrib_value in ('04', '05'))
         group by c.location_name, b.location_id, a.coll_date
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39794602
If there is a difference between the two portions of the union other than the sums vs zeroes, please elaborate, because I don't see it.
0
 
LVL 6

Author Closing Comment

by:anumoses
ID: 39794732
You are correct. That works
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

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…
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 explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

759 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

21 Experts available now in Live!

Get 1:1 Help Now