Jump to content

[SOLVED] Count number of columns which = "Y" for selected rows?


matthewra

Recommended Posts

Hi,

Is it possible to write a query

which counts how many of the columns (named a,b,c,d,e,f,g) have value "Y"?

 

Ideally I would get

select fldAREA, HOWMANYYS from tblDATA GROUP BY fldAREA

HOWMANYYS would be +1 for each of the fields A,b,c,d,e,f,g have value "Y" in that record.

thanks

matthew@kleinosky.com

Link to comment
Share on other sites

I can write

select  if(org_trainp1pres="Y", 1,0) as t1, org_trainp1vped="Y" as t2,  org_trainp1vppr="Y" as t3,

org_area, org_div, org_clubnum  from d60org where org_area <> ""  and org_clubnum <> ''

order by org_div , org_area

 

But I want a result which is the sum of t1+t2+t3

Link to comment
Share on other sites

Hi

 

Not quite sure what you want.

 

First guess is that you want to know each row and a count of how many of the columns for each row contain "Y". This would do it.

 

SELECT (IF(a = 'Y', 1 , 0) + IF(b = 'Y' , 1 , 0) + IF(c = 'Y' , 1 , 0) + IF(d = 'Y' , 1 , 0) + IF(e = 'Y' , 1 , 0) + IF(f = 'Y' , 1 , 0)) AS HOWMANYYS
from tblDATA 

 

If you want to know how many rows have a each have a Y in the various columns:-

 

SELECT 'a' AS fldAREA, COUNT(*)
FROM tblDATA
WHERE a = 'Y'
UNION
SELECT 'b' AS fldAREA, COUNT(*)
FROM tblDATA
WHERE b = 'Y'
UNION
SELECT 'c' AS fldAREA, COUNT(*)
FROM tblDATA
WHERE c = 'Y'
UNION
SELECT 'd' AS fldAREA, COUNT(*)
FROM tblDATA
WHERE d = 'Y'
UNION
SELECT 'e' AS fldAREA, COUNT(*)
FROM tblDATA
WHERE e = 'Y'
UNION
SELECT 'f' AS fldAREA, COUNT(*)
FROM tblDATA
WHERE f = 'Y'

 

Neither seem to point to a particularly logical table.

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.