Monday, July 22, 2013

Left outer join example with three tables along with case





SELECT tab1.tb1_a,
       CASE WHEN tab3.tb3_a IS NOT NULL THEN tb3_b
            ELSE 'No city for him yet'
       END AS City,
tab2.*, tab3.*
FROM tab1
LEFT OUTER JOIN tab2 ON    tab1.tb1_a = tab2.tb2_a
LEFT OUTER JOIN tab3 ON    tab2.tb2_b = tab3.tb3_a
where tb2_b = (select max(tb2_b)
                 from tab2 t22
                where t22.tb2_a = tb1_a)
or    tb2_b is null
order by 1

No comments:

Post a Comment