Problem with SUM syntax

Hi there, I need your appreciated help.

This is my table mysql:


ID	A	P
1	S	
2		S
3	S	S
4	S	S

And this is my query:


SELECT
   SUM(IF(A='S', 1, 0)) 'tot_A'
 , SUM(IF(P='S', 1, 0)) 'tot_P'
   FROM tbl_x;

Output


tot_A	tot_P
3	3

But when I have this records (A and P = S)


ID	A	P
3	S	S
4	S	S

I need SUM only field P value:


tot_A	tot_P
1	3

Can you help me?
Thanks in advance.

So what you’re looking for is “Count A iff* A is equal to ‘S’ and P is ~not~ equal to ‘S’”, which you could do as follows:

[highlight=‘mysql’]
SELECT
SUM(IF(A=‘S’ AND P<>‘S’, 1, 0)) ‘tot_A’
, SUM(IF(P=‘S’, 1, 0)) ‘tot_P’
FROM tbl_x;



That will yield the results you wanted :)

* iff = if and only if

¡Thanks a lot!