Solved

Using WHERE EXISTS still returning duplicate records.  How can I fix this query?

Posted on 2008-06-25
1
217 Views
Last Modified: 2013-12-07
I have this following query:

select u.odf_parent_id,
       u.bhi_eng_mo_upd_yr year1,
       (select SUM(bhi_apexp_period) from ODF_CA_BHI_ENG_MON_UPDT where bhi_eng_mo_upd_yr = u.bhi_eng_mo_upd_yr and odf_parent_id = u.odf_parent_id and bhi_emu_period = 1) PERIOD1,
       (select SUM(bhi_apexp_period) from ODF_CA_BHI_ENG_MON_UPDT where bhi_eng_mo_upd_yr = u.bhi_eng_mo_upd_yr and odf_parent_id = u.odf_parent_id and bhi_emu_period = 2) PERIOD2,
       (select SUM(bhi_apexp_period) from ODF_CA_BHI_ENG_MON_UPDT where bhi_eng_mo_upd_yr = u.bhi_eng_mo_upd_yr and odf_parent_id = u.odf_parent_id and bhi_emu_period = 3) PERIOD3,
       (select SUM(bhi_apexp_period) from ODF_CA_BHI_ENG_MON_UPDT where bhi_eng_mo_upd_yr = u.bhi_eng_mo_upd_yr and odf_parent_id = u.odf_parent_id and bhi_emu_period = 4) PERIOD4,
       (select SUM(bhi_apexp_period) from ODF_CA_BHI_ENG_MON_UPDT where bhi_eng_mo_upd_yr = u.bhi_eng_mo_upd_yr and odf_parent_id = u.odf_parent_id and bhi_emu_period = 5) PERIOD5,
       (select SUM(bhi_apexp_period) from ODF_CA_BHI_ENG_MON_UPDT where bhi_eng_mo_upd_yr = u.bhi_eng_mo_upd_yr and odf_parent_id = u.odf_parent_id and bhi_emu_period = 6) PERIOD6,
       (select SUM(bhi_apexp_period) from ODF_CA_BHI_ENG_MON_UPDT where bhi_eng_mo_upd_yr = u.bhi_eng_mo_upd_yr and odf_parent_id = u.odf_parent_id and bhi_emu_period = 7) PERIOD7,
       (select SUM(bhi_apexp_period) from ODF_CA_BHI_ENG_MON_UPDT where bhi_eng_mo_upd_yr = u.bhi_eng_mo_upd_yr and odf_parent_id = u.odf_parent_id and bhi_emu_period = 8) PERIOD8,
       (select SUM(bhi_apexp_period) from ODF_CA_BHI_ENG_MON_UPDT where bhi_eng_mo_upd_yr = u.bhi_eng_mo_upd_yr and odf_parent_id = u.odf_parent_id and bhi_emu_period = 9) PERIOD9,
       (select SUM(bhi_apexp_period) from ODF_CA_BHI_ENG_MON_UPDT where bhi_eng_mo_upd_yr = u.bhi_eng_mo_upd_yr and odf_parent_id = u.odf_parent_id and bhi_emu_period = 10) PERIOD10,
       (select SUM(bhi_apexp_period) from ODF_CA_BHI_ENG_MON_UPDT where bhi_eng_mo_upd_yr = u.bhi_eng_mo_upd_yr and odf_parent_id = u.odf_parent_id and bhi_emu_period = 11) PERIOD11,
       (select SUM(bhi_apexp_period) from ODF_CA_BHI_ENG_MON_UPDT where bhi_eng_mo_upd_yr = u.bhi_eng_mo_upd_yr and odf_parent_id = u.odf_parent_id and bhi_emu_period = 12) PERIOD12
from  ODF_CA_BHI_ENG_MON_UPDT u  
WHERE EXISTS (SELECT 'X' FROM ODF_CA_BHI_ENG_MON_UPDT x WHERE u.odf_parent_id = x.odf_parent_id AND u.bhi_eng_mo_upd_yr = x.bhi_eng_mo_upd_yr)

Essentially, I am trying to turn the data from vertical to horizontal and have 1 column for each period.  The problem is that it is returning multiple records for the same ODF_PARENT_ID and YEAR.   I don't want to use SELECT DISTINCT since the performance is much slower, but I can't seem to get the EXISTS clause to work properly where it will only return 1 row for each ODF_PARENT_ID / YEAR combination.

As an example of the results:

5022743      2004      0      0      0      719.51      8.44      0      0      99.21      4142.16      0      0      0
5022743      2004      0      0      0      719.51      8.44      0      0      99.21      4142.16      0      0      0
5022743      2004      0      0      0      719.51      8.44      0      0      99.21      4142.16      0      0      0
5022743      2004      0      0      0      719.51      8.44      0      0      99.21      4142.16      0      0      0
5022743      2004      0      0      0      719.51      8.44      0      0      99.21      4142.16      0      0      0
5022743      2004      0      0      0      719.51      8.44      0      0      99.21      4142.16      0      0      0
5022743      2004      0      0      0      719.51      8.44      0      0      99.21      4142.16      0      0      0
5022743      2004      0      0      0      719.51      8.44      0      0      99.21      4142.16      0      0      0
5022743      2004      0      0      0      719.51      8.44      0      0      99.21      4142.16      0      0      0
5022743      2004      0      0      0      719.51      8.44      0      0      99.21      4142.16      0      0      0
0
Comment
Question by:robrubin
[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
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 375 total points
ID: 21867000
try this:
select u.odf_parent_id,
       u.bhi_eng_mo_upd_yr year1
, SUM( CASE WHEN bhi_emu_period = 1 THEN bhi_apexp_period ELSE null end) PERIOD1
, SUM( CASE WHEN bhi_emu_period = 2 THEN bhi_apexp_period ELSE null end) PERIOD2
, SUM( CASE WHEN bhi_emu_period = 3 THEN bhi_apexp_period ELSE null end) PERIOD3
, SUM( CASE WHEN bhi_emu_period = 4 THEN bhi_apexp_period ELSE null end) PERIOD4
, SUM( CASE WHEN bhi_emu_period = 5 THEN bhi_apexp_period ELSE null end) PERIOD5
, SUM( CASE WHEN bhi_emu_period = 6 THEN bhi_apexp_period ELSE null end) PERIOD6
, SUM( CASE WHEN bhi_emu_period = 7 THEN bhi_apexp_period ELSE null end) PERIOD7
, SUM( CASE WHEN bhi_emu_period = 8 THEN bhi_apexp_period ELSE null end) PERIOD8
, SUM( CASE WHEN bhi_emu_period = 9 THEN bhi_apexp_period ELSE null end) PERIOD9
, SUM( CASE WHEN bhi_emu_period = 10 THEN bhi_apexp_period ELSE null end) PERIOD10
, SUM( CASE WHEN bhi_emu_period = 11 THEN bhi_apexp_period ELSE null end) PERIOD11
, SUM( CASE WHEN bhi_emu_period = 12 THEN bhi_apexp_period ELSE null end) PERIOD12
from  ODF_CA_BHI_ENG_MON_UPDT u  
WHERE EXISTS (SELECT 'X' FROM ODF_CA_BHI_ENG_MON_UPDT x WHERE u.odf_parent_id = x.odf_parent_id AND u.bhi_eng_mo_upd_yr = x.bhi_eng_mo_upd_yr)
GROUP BY u.odf_parent_id
, u.bhi_eng_mo_upd_yr

Open in new window

0

Featured Post

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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 syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

717 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