Solved

oracle query

Posted on 2014-01-20
5
331 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 143

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 74

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 74

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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
Queries 15 48
Export table into csv file in oracle 10 255
Oracle - Query link database loop 8 41
oracle DR - data guard failover. 18 48
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
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

830 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