Solved

Oracle query

Posted on 2015-01-27
9
151 Views
Last Modified: 2015-02-02
I have a query

select SUM(CASE
       WHEN da.attrib_value IN ( '04', '05' ) THEN 1
       ELSE 0
       END )
FROM (blood_drives bd
   join donations_don d ON 'DRV'||to_char(bd.drive_id) = d.drive_id
   JOIN sites s on s.site_code = bd.site_code)
   left outer join donation_attributes_don da
     ON ( d.transaction_id = da.transaction_id
    AND da.attrib_value IN ( '04', '05' )
    AND da.attrib_code = 'VENI' )
  WHERE bd.drive_date between :start_date and :end_date
    AND d.unit_id IS NOT NULL
    and area_rep_no = :area_rep_no;

I need to pass this value into a variable called v_count . I tried using into v_count after SUM(CASE
       WHEN da.attrib_value IN ( '04', '05' ) THEN 1
       ELSE 0
       END ) but I am getting an error. Help appreciated. I know it might be a simple and known error that I am getting confused.
0
Comment
Question by:anumoses
  • 5
  • 3
9 Comments
 
LVL 6

Author Comment

by:anumoses
Comment Utility
I am using this query in a report and the error I get I have attached as a screen print. error
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
Remove the parentheses. The query thinks you are selecting from a subquery.
0
 
LVL 6

Author Comment

by:anumoses
Comment Utility
before the blood_drives table?
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
Remove the parentheses before blood_drives bd, after bd.site_code, before d.transaction_id, and after 'VENI'
0
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.

 
LVL 6

Author Comment

by:anumoses
Comment Utility
select SUM(CASE
       WHEN da.attrib_value IN ( '04', '05' ) THEN 1
       ELSE 0
       END ) into v_count2
FROM blood_drives bd
   join donations_don d ON 'DRV'||to_char(bd.drive_id) = d.drive_id
   JOIN sites s on s.site_code = bd.site_code
   left outer join donation_attributes_don da
     ON  d.transaction_id = da.transaction_id
    AND da.attrib_value IN ( '04', '05' )
    AND da.attrib_code = 'VENI'  
  WHERE bd.drive_date between :start_date and :end_date
    AND d.unit_id IS NOT NULL
    and area_rep_no = :area_rep_no;

still get an error

error
0
 
LVL 6

Author Comment

by:anumoses
Comment Utility
I modified the query

select SUM(CASE
       WHEN da.attrib_value IN ( '04', '05' ) THEN 1
       ELSE 0
       END ) into v_count2
from blood_drives bd,
     donations_don d,
     sites s,
     donation_attributes_don da    
where 'DRV'||to_char(bd.drive_id) = d.drive_id
  and s.site_code = bd.site_code
  and d.transaction_id = da.transaction_id
  AND da.attrib_value IN ( '04', '05' )
   AND da.attrib_code = 'VENI'
   and bd.drive_date between :start_date and :end_date
   AND d.unit_id IS NOT NULL
   and area_rep_no = :area_rep_no;

Just need to know if this translates to the old query and that I would get the same result?
0
 
LVL 31

Accepted Solution

by:
awking00 earned 250 total points
Comment Utility
I think you need to state d.transaction_id = da.transaction_id(+) to make it equivalent to the left join in your original query.
0
 
LVL 34

Assisted Solution

by:Mark Geerlings
Mark Geerlings earned 250 total points
Comment Utility
I believe awking00 is correct, in terms of the syntax, but that brings up a logical question for what looks like an inconsistency in your query.   If you need to check two additional columns in that table, then it isn't an outer join any more, because those conditions cannot be true for rows that are missing.
0
 
LVL 6

Author Closing Comment

by:anumoses
Comment Utility
thanks
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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

744 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

16 Experts available now in Live!

Get 1:1 Help Now