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

bonCodigo
  • 14,268
  • 1
  • 48
  • 91
Bryan
  • 17,201
  • 24
  • 97
  • 123

8 Answers8

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
achinda99
  • 5,020
  • 4
  • 34
  • 42
  • 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. – achinda99 Feb 13 '09 at 17:48
  • Brilliant. Wish I'd thought of that. – Andrew Ensley Feb 13 '09 at 17:59
  • 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. – djangofan Jun 17 '11 at 16:35
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)
Kristen
  • 4,227
  • 2
  • 29
  • 36
  • 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. – achinda99 Feb 13 '09 at 18:22
  • 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 – Kristen Feb 13 '09 at 19:25
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;
Community
  • 1
  • 1
achinda99
  • 5,020
  • 4
  • 34
  • 42
  • 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. – achinda99 Feb 27 '09 at 15:30
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.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
  • 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? – Bryan Jul 27 '10 at 17:53
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.

scwagner
  • 3,975
  • 21
  • 16
1

Is this what you wanted?

select distinct c1 from t

JoshBerke
  • 66,142
  • 25
  • 126
  • 164
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.

Quassnoi
  • 413,100
  • 91
  • 616
  • 614
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 
Bryan
  • 17,201
  • 24
  • 97
  • 123