Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

SQL - Multiple tables, multiple columns, one distinct

$
0
0

I'm totally new to DB stuff, so what I have so far is pretty much all based on random stuff I've looked up.

(using SQL Server 2008)

Here is essentially the original query I'm trying to fix:

Text
1
2
3
SELECT col1,col2,col3 FROM table1
UNION
SELECT col1,col2,col3 FROM table2

The problem with that part is that if val1 appears in col1 in both tables, it appears twice if col2 or col3 is different.

The ultimate goal is to select all three columns in both tables but return only rows with unique values in col1. I have no preference for which rows get priority to be displayed (but if it's necessary to specify, let's say best is the bottom value from table1).

So far I can only figure out how to eliminate col1 duplicates if the data is from one table, not two.

Anyone able to help? I doubt this is an uncommon issue, but I'm having trouble finding it. I'm probably not using the right vocabulary anyway, so let me know what to clarify! Thanks



Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>