Solved

oracle query

Posted on 2014-01-20
5
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

752 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