Friday, 12 August 2016

MYSQL Query - Count of Same values in all the columns in each row

field1field2field3field4field5field6field7field8field9field10field11id
NNNYYNNNNNN5343
NNNYYNNNNNN5343
NNNYYNNNNNN5343
NNNYYNNNNNN5343
NNNNYNNNNNN5357
NNNNYNNNNNN5357
NNNNYNNNNNN5357
NNNNYNNNNNN5357
NNYYYNNNNNN5358
NNYYYNNNNNN5358
NNYYYNNNNNN5358
NNYYYNNNNNN5358
NNNNNNNNNNN5361
NNNNNNNNNNN5361
NNNNNNNNNNN5361
NNNNNNNNNNN5361
NNNNNNNNNNN5362
NNNNNNNNNNN5362
NNNNNNNNNNN5362
NNNNNNNNNNN5362
NNNNNNNNNNN9475
NNNNNNNNNNN9475
NNNNNNNNNNN9475
NNNNNNNNNNN9476
NYNNNNNYYNN9476
NYNNNNNYYNN9476
NNNYYNNNNNN5366
NNNYYNNNNNN5366
NNNYYNNNNNN5366
NNNYYNNNNNN5366
NNNNYNNNNNN5367
NNNNYNNNNNN5367
NNNNYNNNNNN5367
NNNNYNNNNNN5367
NNNNNNYNNNN5369
NNNNNNYNNNN5369
NNNNNNYNNNN5369
NNNNNNYNNNN5369
NNNNNNYNNNN5369
NNNNNNYNNNN5369
NNNNNNYNNNN5369
NNNNNNYNNNN5369
NNNNNNYNNNN5369
NNNNNNYNNNN5369
NNNNNNYNNNN5369
NNNNNNNNNNN5369
NNYYYNNNNNN5370
NNYYYNNNNNN5370
NNYYYNNNNNN5370
NNYYYNNNNNN5370




I have above table and I want to find out how many "Y" are present in each row. In the above table each column contains two values only. That is "Y" OR "N".


Answere:

SELECT field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11,id, IF(field1= 'Y',1,0) + IF(field2= 'Y',1,0) +IF(field3= 'Y',1,0) + IF(field4= 'Y',1,0) +IF(field5= 'Y',1,0) + IF(field6= 'Y',1,0) +IF(field7= 'Y',1,0) + IF(field8= 'Y',1,0) +IF(field9= 'Y',1,0) + IF(field10= 'Y',1,0) +IF(field11= 'Y',1,0)  as testcolumn,LENGTH(REPLACE(CONCAT( field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11),'N','')) as len FROM member_stats LIMIT 50;

field1field2field3field4field5field6field7field8field9field10field11idtestcolumnlen
NNNYYNNNNNN534322
NNNYYNNNNNN534322
NNNYYNNNNNN534322
NNNYYNNNNNN534322
NNNNYNNNNNN535711
NNNNYNNNNNN535711
NNNNYNNNNNN535711
NNNNYNNNNNN535711
NNYYYNNNNNN535833
NNYYYNNNNNN535833
NNYYYNNNNNN535833
NNYYYNNNNNN535833
NNNNNNNNNNN536100
NNNNNNNNNNN536100
NNNNNNNNNNN536100
NNNNNNNNNNN536100
NNNNNNNNNNN536200
NNNNNNNNNNN536200
NNNNNNNNNNN536200
NNNNNNNNNNN536200
NNNNNNNNNNN947500
NNNNNNNNNNN947500
NNNNNNNNNNN947500
NNNNNNNNNNN947600
NYNNNNNYYNN947633
NYNNNNNYYNN947633
NNNYYNNNNNN536622
NNNYYNNNNNN536622
NNNYYNNNNNN536622
NNNYYNNNNNN536622
NNNNYNNNNNN536711
NNNNYNNNNNN536711
NNNNYNNNNNN536711
NNNNYNNNNNN536711
NNNNNNYNNNN536911
NNNNNNYNNNN536911
NNNNNNYNNNN536911
NNNNNNYNNNN536911
NNNNNNYNNNN536911
NNNNNNYNNNN536911
NNNNNNYNNNN536911
NNNNNNYNNNN536911
NNNNNNYNNNN536911
NNNNNNYNNNN536911
NNNNNNYNNNN536911
NNNNNNNNNNN536900
NNYYYNNNNNN537033
NNYYYNNNNNN537033
NNYYYNNNNNN537033
NNYYYNNNNNN537033

0 comments:

Post a Comment