mercredi 29 juin 2016

What table a record came from and if more than one table

I have 4 tables (Table1 through Table4) and column CODE in each table. I use a UNION ALL to identify where they come from

select  distinct CODE, 'Table1'  from Table1 where TABLE1_EFF_STARt >= '01/01/2014'
union all
select  distinct CODE, 'Table2' from Table2 where TABLE2_EFF_STARt >= '01/01/2014'
union all
select  distinct CODE, 'Table3' from Table3 where TABLE3_EFF_STARt >= '01/01/2014'
union all
select  distinct CODE, 'Table4' from Table4 where TABLE4_EFF_STARt >= '01/01/2014' 

Sometimes the same CODE can be in more than one table, and I want to identify and return the result in a below table form. When code in more than one table I want to flag it as Y in the Multisource column.

CODE    Table1  Table2  Table3  Table4  Multisource

12345   Y       N       N       N       N
34233   Y       Y       N       N       Y
45345   Y       N       N       N       N
23417   Y       N       Y       N       Y
45346   Y       N       N       N       Y

Aucun commentaire:

Enregistrer un commentaire