0

please help me for this query, i have this data

customeid | param
1001      |  A
1001      |  B
1001      |  C
1002      |  B
1002      |  A
1003      |  A
1003      |  B
1004      |  A

i need to distinct customeid, i use this query but the result not correct

SELECT
  COUNT(DISTINCT IF(param not in ('A'),1,0))
FROM
  table

the output result

count   
  3 (from customeid 1001,1002,1003)

how to distinct customeid with if param not in A and ican't add where query

4
  • 1
    An output table would help Commented Jul 19, 2018 at 7:09
  • expected output is 3? Commented Jul 19, 2018 at 7:23
  • yes i want to count customeid distinct where param != A but i can't use where query, because this is only part of my select query. Commented Jul 19, 2018 at 7:26
  • count must be 3 for your table if i am right? Commented Jul 19, 2018 at 7:49

4 Answers 4

1

You use count if with distinct to judge that there is no problem, but there are issues, If the condition is met You need to return customeid, otherwise you need to return null

You can follow this query.

SELECT
  COUNT(DISTINCT IF(param not in ('A'),customeid,NULL))
FROM
  table

sqlfiddle

NOTE

COUNT won't accumulate when that value is null

Sign up to request clarification or add additional context in comments.

1 Comment

No problem :),Glad to help
0
select customeid,count(customeid)
from table
where param <>'A'
group by customeid

Comments

0

use case when and aggregate function group by

 select customeid,count(*) as Count
 from (    
    select (case when param!='A' then customeid else end) as customeid    from your_table
) T group by customeid

1 Comment

@Denny R.z does it help
0
SELECT DISTINCT COSTUMEID, COUNT(DISTINCT (PARAM))
FROM TABLE 
WHERE PARAM NOT IN (('A'),1,0) 
GROUP BY COSTUMEID

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.