?
Solved

oracle query

Posted on 2014-01-20
5
Medium Priority
?
334 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 2000 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 Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
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.

719 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