计算不同付费用户的数量

时间:2018-02-05 00:12:01

标签: mysql sql

这是我的输入表格(标签),其中包含用户ID和“已验证购买”。 (用户可以尝试购买,但可以决定不购买。因此验证购买)

UserID   Verified_Purchase
 1001          T
 1001          T 
 1001          F   
 1002          T 
 1002          T
 1003          F
 1004          T
 1004          F


My expected output is to find out the no of unique buyers / non buyers. 
No of Buyers :  3  (1001,1002,1004)
No of Non Buyers : 1 (only 1003)

我尝试过:

select distinct UserID from tab
WHERE Verified_Purchase = 'T'

select distinct UserID from tab
WHERE Verified_Purchase = 'F'

但由于很少有用户被重复计算,因此这些答案并没有给出正确答案。

请让我知道如何继续这样做。

1 个答案:

答案 0 :(得分:1)

首先,按userId汇总,然后汇总:

select is_verified, count(*) as num_users
from (select UserId, max( Verified_Purchase = 'T' ) as is_verified
      from tab t
      group by UserId
     ) t
group by is_verified;