Select distinct from multiple fields using sql

68,587

Solution 1

This should give you all distinct values from the table. I presume you'd want to add where clauses to select only for a particular question. However, this solution requires 5 subqueries and can be slow if your table is huge.

SELECT DISTINCT(ans) FROM (
    SELECT right AS ans FROM answers
    UNION
    SELECT wrong1 AS ans FROM answers
    UNION
    SELECT wrong2 AS ans FROM answers
    UNION
    SELECT wrong3 AS ans FROM answers
    UNION
    SELECT wrong4 AS ans FROM answers
) AS Temp

Solution 2

SELECT DISTINCT(ans) FROM (
    SELECT right AS ans FROM answers
    UNION
    SELECT wrong1 AS ans FROM answers
    UNION
    SELECT wrong2 AS ans FROM answers
    UNION
    SELECT wrong3 AS ans FROM answers
    UNION
    SELECT wrong4 AS ans FROM answers
) AS Temp

The DISTINCT is superfluous, because the UNION will not return rows that are identical for all columns. (When you want duplicated, or if you know that no duplicates exist, use UNION ALL for faster performance)

This will give you a single list containing all the answers. You'll still have duplicates though if you have multiple copies of the same answer within a single column.

That should not be the case if you use UNION, only if you use UNION ALL

SELECT [value] INTO #TEMP
FROM
(
SELECT  [value] = 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 1
) AS X

(4 row(s) affected)

SELECT [value] 
FROM    #TEMP

value       
----------- 
1
2
3
1

(4 row(s) affected)

SELECT [value] 
FROM    #TEMP
UNION 
SELECT [value]
FROM    #TEMP

value       
----------- 
1
2
3

(3 row(s) affected)

Solution 3

I provided one answer above.

However I figured a much better way to do it using UNPIVOT.

SELECT DISTINCT(ans)
FROM (
    SELECT [Name], ANS 
    FROM (
        SELECT right, wrong1, wrong2, wrong3, wrong4 
        FROM answers
    ) AS PVT
    UNPIVOT 
    (ans FOR [Name] IN (right, wrong1, wrong2, wrong3, wrong4)) AS UNPVT
) AS OUTPUT;

You can provide any WHERE clause in the internal subquery:

SELECT DISTINCT(ans)
FROM (
    SELECT [Name], ANS 
    FROM (
        SELECT right, wrong1, wrong2, wrong3, wrong4 
        FROM answers
        WHERE (...)
    ) AS PVT
    UNPIVOT 
    (ans FOR [Name] IN (right, wrong1, wrong2, wrong3, wrong4)) AS UNPVT
) AS OUTPUT;

Solution 4

Columns of "right, wrong1, wrong2, wrong3, wrong4" mean that you have a mis-designed database. In general, a number or letter suffix on a column name should be a red flag to rethink the problem.

As you observed, your design required you to hack around to get a solution to a typical data reduction problem.

Solution 5

Well you can use a UNION and run 5 select statements, one for each column in your table. It would look something like this:

SELECT right FROM answers
UNION
SELECT wrong1 FROM answers
UNION
SELECT wrong2 FROM answers
UNION
SELECT wrong3 FROM answers
UNION
SELECT wrong4 FROM answers

This will give you a single list containing all the answers. You'll still have duplicates though if you have multiple copies of the same answer within a single column.

Share:
68,587
Bryan
Author by

Bryan

Updated on July 05, 2022

Comments

  • Bryan
    Bryan almost 2 years

    I have 5 columns corresponding to answers in a trivia game database - right, wrong1, wrong2, wrong3, wrong4

    I want to return all possible answers without duplicates. I was hoping to accomplish this without using a temp table. Is it possible to use something similar to this?:

    select c1, c2, count(*)
    from t
    group by c1, c2
    

    But this returns 3 columns. I would like one column of distinct answers.

    Thanks for your time

  • Rabeel
    Rabeel over 15 years
    Hard to tell if it is, but +1 cause it looks like it should be :)
  • Bryan
    Bryan over 15 years
    No, that selects distinct from one column
  • JoshBerke
    JoshBerke over 15 years
    Ahh ok so I understand your question now...union is your best bet;-)
  • achinda99
    achinda99 over 15 years
    Try the pivot one I just did below. I think that's much better. I'd read on whats more efficient though, I don't know too much about the internal functioning of the different database servers.
  • Andrew Ensley
    Andrew Ensley over 15 years
    Brilliant. Wish I'd thought of that.
  • achinda99
    achinda99 over 15 years
    Yeah, you're right. I was wrong on that. I tested both and it gave me similar results on my dataset but I wasn't sure of the specifics so if there were duplicates DISTINCT would have taken care of it. I've never used a UNION in any real work I've done. Just educational purposes.
  • Kristen
    Kristen over 15 years
    No worries! Worth remembering to always use UNION ALL when you know that there are no duplicates (or you WANT any duplicates). Saves the server having to do a SORT and De-DUPE
  • Bryan
    Bryan almost 14 years
    You are a goofball. For a simple quiz there is nothing wrong with creating a quick table of questions. Trying to sound smarter than... we are are we?
  • djangofan
    djangofan almost 13 years
    This doesn't work for me on SQL 2005 since the query is trying to convert nvarchar values into int on each of the AS ans statements, and obviously that fails. Using CAST didn't help.