Solved

oracle query

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle - Create Procedure with Paramater 16 63
1 FROM DUAL wont work with additional columns ?? 4 36
use lov values 2 49
Oracle Verification of DataPump Export and Import 17 55
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 …
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…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to recover a database from a user managed backup

776 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