sql - select UNION except one column -


i have question:

i want join 2 sql querys 1 query using union avoid duplicates, need know if data comes fisrt select query or second select query.

sample data:

 table                                                b table ----------------------------------------------------------------------------- 01 john                                                01 john 02 juan                                                02 peter 03 martin                                              03 martin 

i have this:

select a.code,a.name conditions unión select b.code,b.name b diferent conditions 

result table


    01 john                                                     02 juan       02 peter     03 martin 

this works fine, if want know if data comes first query or second think this:

select a.code,a.name, 'a'   conditions unión select b.code,b.name, 'b'   b diferent conditions 

result table


    01 john  'a'                                                   01 john  'b'     02 juan  'a'     02 peter 'b'     03 martin 'a'     03 martin 'b' 

but don't avoid "duplicates" because 'a' diferent 'b', question is, can don't compare 'a' 'b'?, way obtain expected result?

edit:

the expected result


    01 john  'a'                                                   02 juan  'a'     02 peter 'b'     03 martin 'a' 

here's way of doing it:

select code, name, min(sourcetable) sourcetable (   select code, name, 'a' sourcetable               union     select code, name, 'b' sourcetable            b) t group code, name  order code 

demo here

or perhaps:

select code, name, sourcetable (   select code, name, sourcetable,          row_number() on (partition code, name                              order sourcetable) rn   (     select code, name, 'a' sourcetable                   union      select code, name, 'b' sourcetable              b) t) x x.rn = 1   

demo here


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -