Is there a better/more concise way to write this sql query?
Instead of using 'case' as many times as i do?
Code:
select
case
when b.patient_id is null then a.dfcode else b.dfcode
end as dfcode,
case
when b.patient_id is null then a.diagcode else b.diagnosis
end as diasgnosis,
case
when b.patient_id is null then a.times_testing else b.times_testing
end as times_testing,
case
when b.patient_id is null then a.on_insulin else b.insulin
end as insulin
from event.dbo.patient_dg() a
left outer join event.dbo.diabetes_rx_tracker b on a.code=b.patient_id and b.image_id='2110180'
where a.code='ADSMANZS01'
Looks like IFNULL() could contribute to some conciseness.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
select ISNULL(a.dfcode, b.dfcode) as dfcode,
ISNULL(a.diagcode, b.diagcode) as diagcode,
ISNULL(a.times_testing, b.times_testing) as times_testing,
ISNULL(a.on_insulin, b.on_insulin) as on_insulin,
from event.dbo.patient_dg() a
left outer join event.dbo.diabetes_rx_tracker b on a.code=b.patient_id and b.image_id='2110180'
where a.code='ADSMANZS01'
Bookmarks