Solved

Oracle query

Posted on 2015-01-27
9
163 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
[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
  • 5
  • 3
9 Comments
 
LVL 6

Author Comment

by:anumoses
ID: 40572671
I am using this query in a report and the error I get I have attached as a screen print. error
0
 
LVL 32

Expert Comment

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

Author Comment

by:anumoses
ID: 40572692
before the blood_drives table?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 32

Expert Comment

by:awking00
ID: 40572705
Remove the parentheses before blood_drives bd, after bd.site_code, before d.transaction_id, and after 'VENI'
0
 
LVL 6

Author Comment

by:anumoses
ID: 40572733
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
ID: 40572771
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 32

Accepted Solution

by:
awking00 earned 250 total points
ID: 40572947
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 35

Assisted Solution

by:Mark Geerlings
Mark Geerlings earned 250 total points
ID: 40573334
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
ID: 40583916
thanks
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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 setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

627 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