robrubin
asked on
Using WHERE EXISTS still returning duplicate records. How can I fix this query?
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.