sql - Replacing In clause with exists -
hi gurus, i'm looking replace in clause exists, despite reading other similar cases on here i've not been able apply them dataset.
i looking add in column main query tells me if fund found within separate list, , if label 'emergency' , if not 'non-emergency'
the list defined so:
select f.id _audit inner join _fund f on a.article_id = f.id a.entity_name = 'fund' , a.changes '%finance_code2%oldvalue>3%' ) union ( select id fund_reference _fund (finance_code2 '3%' or finance_code2 '9%') , finance_code2 not null
and looking like:
select ...main query here... ,case when fund_id in (list_details) 'emergency' else 'non-emergency' end
i know more efficient like
select ...main query here... ,select case when exists (select fund_id list_details fund_id not null) 'emergency' else 'non-emergency' end
but every time try keeps returning false values (saying funds contained within list when not)
in case helps i'm using sql server 2005 , main query listed below, list_details result (id) joined onto donation_fund_allocation on list_details.id = donation_fund_allocation.fund_id
as clue massively appreciated :) thanks!
main query
select don.supporter_id contact_id ,don.id gift_id ,year(don.date_received) calendar_year ,year(don.date_received) - case when month(don.date_received) < 4 1 else 0 end financial_year ,don.date_received date_received ,don.event_id event_id ,sum(case when don.gift_aid_status <> 4 don.value_gross * ((dfa.percentage) / 100) when don.gift_aid_status = 4 , don.value_net > don.value_gross , don.value_net <> 0 don.value_net * ((dfa.percentage) / 100) else don.value_gross * ((dfa.percentage) / 100) end ) donation_value --**list details query go in here** donation don (nolock) inner join donation_fund_allocation dfa (nolock) on dfa.donation_id = don.id don.supporter_id not null , don.status = 4 , don.value_gross <> 0 group don.supporter_id ,don.id ,don.date_received ,don.event_id
you need correlate exists call outer query. written asking if there exist any rows in list_details fund_id isn't null
so, want is
select ...main query here... ,select case when exists (select 1 list_details fund_id = outer.fund_id) 'emergency' else 'non-emergency' end
where outer
table alias fund_id
can found in main select
Comments
Post a Comment