Suppose we have a dataset as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
CREATE TABLE #temp ( NUMBER INT, NAME VARCHAR(1) ) INSERT INTO #temp VALUES (1,'a'), (1,'a'), (1,'b'),(2,'a'), (2,'b'),(2,'b'), (1,'c'),(2,'c') SELECT * from #temp |
1 2 3 4 5 6 7 8 9 |
NUMBER NAME 1 a 1 a 1 b 2 a 2 b 2 b 1 c 2 c |
DISTINCT will return only unique value. For Number, it’s 1 and 2. For Name, it’s a, b, and c.
1 2 |
SELECT DISTINCT NUMBER FROM #temp SELECT DISTINCT NAME FROM #temp |
1 2 3 |
NUMBER 1 2 |
1 2 3 4 |
NAME a b c |
But if we want unique combinations, this code will do the trick.
1 2 |
SELECT DISTINCT NUMBER, NAME FROM #temp |
1 2 3 4 5 6 7 |
NUMBER NAME 1 a 1 b 1 c 2 a 2 b 2 c |