12

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

8 Answers 8

15

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
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Brilliant. Wish I'd thought of that.
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.
8
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)

2 Comments

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.
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
5

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;

1 Comment

I like this one better because logically, I'm doing what I want to do. The first query just achieves it, where as this follows my though process. Also, less select subqueries.
2

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.

Comments

2

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.

1 Comment

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?
1

Is this what you wanted?

select distinct c1 from t

3 Comments

Hard to tell if it is, but +1 cause it looks like it should be :)
No, that selects distinct from one column
Ahh ok so I understand your question now...union is your best bet;-)
0

In MySQL AND MS SQL:

SELECT
      CASE
        WHEN which = 1 THEN c1
        WHEN which = 2 THEN c2
        WHEN which = 3 THEN c3
        WHEN which = 4 THEN c4
        WHEN which = 5 THEN c5
      END AS answer,
      which
FROM mytable, (
     SELECT 1 AS which
     UNION ALL 
     SELECT 2
     UNION ALL 
     SELECT 3
     UNION ALL 
     SELECT 4
     UNION ALL 
     SELECT 5
) w

For Oracle, add FROM DUAL to each of the number selects.

Comments

0

This is the exact answer.

SELECT (ans) FROM (
    SELECT correct AS ans FROM tGeoQuiz 
    UNION
    SELECT wrong1 AS ans FROM tGeoQuiz 
    UNION
    SELECT wrong2 AS ans FROM tGeoQuiz
    UNION
    SELECT wrong3 AS ans FROM tGeoQuiz
    UNION
    SELECT wrong4 AS ans FROM tGeoQuiz 
) AS Temp 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.