Link to home
Start Free TrialLog in
Avatar of hbojorquezgeek
hbojorquezgeek

asked on

One Table, Multiple Count(*), MySql

Ok,
This is what I have ..

Table viHomeVisitbyPara
ParaID | Week1 | Week2 | Week3  |  Week4 | Week5 |  Week6 |  (all the way to Week30
2         |   1       |     2      |       3       |    4       |   Null   |              |
4         |  1        |      2     |  Null        |        Null     |


You get the picture

I'm looking for  a query what will give me a count of those ParaID  that have a certain number for a certain week-- but I want this count all the way to Week30

If it was just one week.. well
it would be easy

SELECT COUNT(*) as Week1Count FROM viHomeVisitsbyPara WHERE Week1=1  

How can you do this for 30 columns?
Avatar of Aleksandar Bradarić
Aleksandar Bradarić
Flag of Serbia image

I'm not sure I understand. Could you please explain... Do you need something like:
---
SELECT COUNT(*) as Week1Count FROM viHomeVisitsbyPara WHERE Week1=1 OR Week2=1 OR ... OR Week30=1
---

Some sample table data with a couple of rows and the expected result would be great :)
Avatar of hbojorquezgeek
hbojorquezgeek

ASKER

The expected resut would be something like

ParaID  | Week1Count  |    Week2Count  |    Week3Count  |
     3      |      2              |             1          |           1              |
     2      |       4             |              1          |           0              |

The table data would be
ParaID   | Week1  |    Week2    |   Week3  |
 3          |  1           |   2           |    3          |
 3          |  1           |    Null        |    Null     |
 2          |  1           |   2           |    Null       |
 2          |  1           |    Null        |    Null      |
 2          |  1           |    Null        |    Null      |
 2          |  1           |    Null        |    Null      |
 

As you can see I want something that summarizes wether a specific number is recorded  under each week... a 1 for Week1, 2 for Week2, etc...

SELECT ParaID,COUNT(Week1) as Week1Count, COUNT(Week2) as Week2Count,COUNT(Week3) as Week3Count FROM viHomeVisitsbyPara WHERE Week1=1 OR Week2=2 OR Week3=3 GROUP BY ParaID
Steelseth12...
nope..
That returned completely wrong tallies.
It returned
ParaID  | Week1Count  |    Week2Count  |    Week3Count   |
     3      |      2              |             2            |           2              |
     2      |       4             |             4            |           4              |
I see :) It's quite simple. Try this:
---
SELECT
  ParaID,
  COUNT(Week1) as Week1Count,
  COUNT(Week2) as Week2Count,
  COUNT(Week3) as Week3Count
FROM
  viHomeVisitsbyPara v
GROUP BY
  ParaID
---
By the way, there will have to be the NULL values in there for this to work. Here's a version that works with both NULLs and zeros:
---
SELECT
  ParaID,
  SUM(IF(Week1 IS NULL, 0, IF(Week1 = 1, 1, 0))) as Week1Count,
  SUM(IF(Week2 IS NULL, 0, IF(Week2 = 2, 1, 0))) as Week2Count,
  SUM(IF(Week3 IS NULL, 0, IF(Week3 = 3, 1, 0))) as Week3Count
FROM
  viHomeVisitsbyPara v
GROUP BY
  ParaID
---
The query i gave above ...

>> SELECT ParaID,COUNT(Week1) as Week1Count, COUNT(Week2) as Week2Count,COUNT(Week3) as Week3Count FROM viHomeVisitsbyPara WHERE Week1=1 OR Week2=2 OR Week3=3 GROUP BY ParaID

on the table data you gave

>>

ParaID   | Week1  |    Week2    |   Week3  |
 3          |  1           |   2           |    3          |
 3          |  1           |    Null        |    Null     |
 2          |  1           |   2           |    Null       |
 2          |  1           |    Null        |    Null      |
 2          |  1           |    Null        |    Null      |
 2          |  1           |    Null        |    Null      |
 

RETURNS

ParaID  | Week1Count  |    Week2Count  |    Week3Count  |
     2      |       4             |              1          |           0              |
     3      |      2              |             1          |           1              |

which is what you say you  want.
   
leannon's answer worked
... but I have a question..
Why are the argument of the second IF statement   --- 1,1,0?

  SUM(IF(Week1 IS NULL, 0, IF(Week1 = 1, 1, 0))) as Week1Count,
 
> SUM(IF(Week1 IS NULL, 0, IF(Week1 = 1, 1, 0))) as Week1Count,

The `IF(Week1 = 1, 1, 0)` translates to: `if the value for Week1 equals 1, then add 1 to the sum/count, else add 0`. It makes the query count only the items where `Week1 = 0`.

You can also modify it to say: `IF(Week1 > 0, 1, 0)`, which would count any non-zero number in the `Week1` field.
ASKER CERTIFIED SOLUTION
Avatar of Aleksandar Bradarić
Aleksandar Bradarić
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial