I'm doing some basic sql on a few tables I have, using a union(rightly or wrongly)
but I need remove the duplicates. Any ideas?
select * from calls
left join users a on calls.assigned_to= a.user_id
where a.dept = 4
union
select * from calls
left join users r on calls.requestor_id= r.user_id
where r.dept = 4
SQL Union All Operator Overview The SQL Union All operator combines the result of two or more Select statement similar to a SQL Union operator with a difference. The only difference is that it does not remove any duplicate rows from the output of the Select statement.
Union will remove duplicates. Union All does not.
UNION: only keeps unique records. UNION ALL: keeps all records, including duplicates.
Anyway, the answer to this question is simple, though both UNION and UNION ALL are used to combine the result of two separate SQL queries on the same or different table, UNION does not keep a duplicate record (a row is considered duplicate if the value of all columns is same), while UNION ALL does.
Union
will remove duplicates. Union All
does not.
Using UNION
automatically removes duplicate rows unless you specify UNION ALL
:
http://msdn.microsoft.com/en-us/library/ms180026(SQL.90).aspx
Others have already answered your direct question, but perhaps you could simplify the query to eliminate the question (or have I missed something, and a query like the following will really produce substantially different results?):
select *
from calls c join users u
on c.assigned_to = u.user_id
or c.requestor_id = u.user_id
where u.dept = 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With