Combining four tables in SQL Server -
i have 4 tables table a, table b, table c , table d. schema of 4 tables identical. need union these 4 tables in following way:
if record present in
table aconsidered in output table.if record present in
table bconsidered in output table if not present intable a.if record present in
table cconsidered if not present intable a,table b.if record present in
table dconsidered if not present intable a,table b, ,table c.
note -
every table has column identifies table every record (i don't know if of importance)
records identified based on particular column -
column xnot unique within each table
you (only 2 cases shown should see how extend this)
with cte1 ( select 't1' source, x, y t1 union select 't2' source, x, y t2 ), cte2 ( select *, rank() on (partition x order case source when 't1' 1 when 't2' 2 end) rn cte1 ) select x,y cte2 rn=1
Comments
Post a Comment