Answers for "Distinct column from union of multiple table and multiple columns"
https://ask.sqlservercentral.com/questions/25324/distinct-column-from-union-of-multiple-table-and-m.html
The latest answers for the question "Distinct column from union of multiple table and multiple columns"Answer by Graham 2
https://ask.sqlservercentral.com/answers/25353/view.html
<p>Thanks, after much head scratching I got it to work using a sub-select</p>
<pre>
select s.EML , max(s.Z) max(s.FN) from
(
select EML, FN, 'Y' Z from table1
union distinct
select EML, FN, 'N' Z from table2
union distinct
select EML, FN, 'N' Z from table3
order by Z DESC
)
as s group by s.EML
</pre>
<p>It's the max on the Z (Y/N) that gives the preference on the first table.</p>
<p>Hope this helps someone else.</p>Mon, 29 Mar 2010 04:54:02 GMTGraham 2Answer by Squirrel 1
https://ask.sqlservercentral.com/answers/25331/view.html
<pre><code>select EML, FN, 'Y' Z from table1 t1
union distinct
select EML, FN, 'N' Z from table2 t2
where not exists (select * from table1 x where x.EML = t2.EML and x.FN = t2.FN)
union distinct
select EML, FN, 'N' Z from table3
where not exists (select * from table1 x where x.EML = t3.EML and x.FN = t3.FN)
and not exists (select * from table2 x where x.EML = t3.EML and x.FN = t3.FN)
order by Z DESC
</code></pre>Thu, 25 Mar 2010 19:39:49 GMTSquirrel 1