I'm having a hard time getting joins to communicate with each other when I have a subquery in one of them. For example I have the below query and I need the term in the 2nd outer join to equal the term prompted for in the first join. And of course, the subquery doesn't see the prompted for term value. How do I get around this.
select T1.id, T1.year, T1.term, T1.other_stuff, TH.term, A.CHARGES
from table1 T1
left outer join t_hist TH on T1.id = TH.id
and TH.term = <user prompted for term>
left outer join (select T2.id CASE when (sum(TH2.amt) > 0
and (TH2.code in ('X1','Y1')))
then (sum(TH2.amt))
else 0.0
end as 'CHARGES'
FROM table1 T2, t_hist TH2
WHERE T2.id = TH2.id
AND TH2.term = TH.term <<<< Need to match TH.term from above join
GROUP BY T2.id, TH2.code) A
ON T2.id = A.id
Thank you.
select T1.id, T1.year, T1.term, T1.other_stuff, TH.term, A.CHARGES
from table1 T1
left outer join t_hist TH on T1.id = TH.id
and TH.term = <user prompted for term>
left outer join (select T2.id CASE when (sum(TH2.amt) > 0
and (TH2.code in ('X1','Y1')))
then (sum(TH2.amt))
else 0.0
end as 'CHARGES'
FROM table1 T2, t_hist TH2
WHERE T2.id = TH2.id
AND TH2.term = TH.term <<<< Need to match TH.term from above join
GROUP BY T2.id, TH2.code) A
ON T2.id = A.id
Thank you.