sql - INTERSECT on a particular column or excluding a column -
been while since i've played in sql world , use hand trying solve 1 haven't been able work out how yet.
i have query joining lot of results using union
, removing using intersect
such as:
(query 1 union query 2 union query 3) intersect query 4
my problem have computed column in each of queries 1-3 denotes query came from.
i.e.
col 1 | col 2 | col3 .... q1 | | b .... q2 | | d .... q3 | e | d ....
the union of these fine, when want intersect query cannot seem find way ignore column.
so question is: how can ignore column when doing intersect, or intersect on specified column?
note: incorporate q4 each of other queries seems lot of unnecessary duplication.
you can use inner join
instead of intersect
:
select q.col1 uq, x.col1 iq, q.col2, q.col3 ( select * (values ('q1', 'a', 'b')) q(col1, col2, col3) union select * (values ('q2', 'a', 'd')) q(col1, col2, col3) union select * (values ('q3', 'e', 'd')) q(col1, col2, col3) ) q(col1, col2, col3) inner join (values ('q4', 'e', 'd')) x(col1, col2, col3) on q.col2 = x.col2 , q.col3 = x.col3
simply include in on
clause of inner join
columns involved in intersection.
output above query is:
uq iq col2 col3 =================== q3 q4 e d
Comments
Post a Comment