views:

109

answers:

5

Hi I have a table with two columns:

column A    column B
  1             2
  1             2 
  2             1

I want to return total of ones = 3 total of twos = 3

The best I can come up with is two queries like so:

SELECT sum(CASE WHEN columnA =1 THEN 1 ELSE 0  END ) 
     + sum(CASE WHEN columnB =1 THEN 1 ELSE 0 END ) 

SELECT sum(CASE WHEN columnA =2 THEN 1 ELSE 0  END ) 
     + sum(CASE WHEN columnB =2 THEN 1 ELSE 0 END ) 

Can this be done in one query? Thanks

A: 

In general, you would count things like so:

SELECT columnA, COUNT(*) FROM myTable
GROUP BY columnA

to get the count of all different values in columnA.

Joel L
+1 I would add `WHERE columnA IN (1,2)`
soulmerge
A: 

To get everything in one query, I would try something like this.

SELECT Result.Val, COUNT(Result.Val) AS Count
FROM (
    SELECT ColumnA AS Val
    FROM TableName

    UNION

    SELECT ColumnB AS Val
    FROM TableName
) AS Result
GROUP BY Result.Val
mcliedtk
I'm getting syntax error with this. I dont know if I've understood it correctly. Result and val are used asis?
bsandrabr
Let me take a look. I'm used to working in SQL Server so perhaps I was a little optimistic at how this may translate to MySQL.
mcliedtk
I confirmed that this works in MS SQL Server, but I am unable to test this in MySQL. So anyone who may be interested in this answer, please be aware that this solution may only work in SQL Server. I'll leave it on here in case it may prove helpful to someone.
mcliedtk
+2  A: 

You didn't specify if you want to do this as 2 rows or as 2 values in a row.

Two rows are somewhat obvious (just union together all the values from each columns, and count(1) group by value against the result of the union; so I'll assume you want to do one row.

If you only have 1s or 2s, it's simple:

SELECT SUM(A+B-2) 'twos', SUM(4-A-B) 'ones' FROM myTable
DVK
can you explain that for me, I don't get the 4
bsandrabr
If A=B=1, then the first sum will contain 0 (thus not counting towards twos) and second sum will contain 2 (thus counting 2 ones)
DVK
If A=B=2, then the first sum will contain 2 (thus counting 2 twos) and second sum will contain 0 (thus counting zero ones)
DVK
If A and B are 1 and 2 in any order, then the first sum will contain 1 (thus counting 1 towards twos) and second sum will contain 1 (thus counting 1 ones)
DVK
Sorry for confising you with mis-typing the labels in wrong order :)
DVK
+1: Ingenious - but is it generalizable?
Jonathan Leffler
Thanks for the extra explanation after 15 mins of reading this I finally understand..... er I think
bsandrabr
@Jonathan - Depends on the exact problem domain. It's generalizable to an extent, but obviously not as generic as the obvious count(*) one (probably even less than the multi-case one). I just find it a lot more interesting for this specific problem :)
DVK
@bsandrabr - it may help grokking this if you simply run the calculations by hand in a combination table (A, B, ones, twos, for every combination of 1s and 2s)
DVK
A: 
SELECT COUNT(*) FROM table WHERE columnA=1 or columnB=1
Jan Tojnar
Thanks, but what about the twos?
bsandrabr
+1  A: 
SELECT SUM(IF(columnA=1, 1, 0) + IF(columnB=1, 1, 0)) as ones,
    SUM(IF(columnA=2, 1, 0) + IF(columnB=2, 1, 0)) as twos
FROM myTable;

C.

symcbean
Thanks thats the solution that worked and I'm just about capable of understnding
bsandrabr