mysql - 统计付费用户的不同数量

标签 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'

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

请告诉我如何继续进行同样的操作。

最佳答案

首先按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;

关于mysql - 统计付费用户的不同数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48614284/

相关文章:

jquery - 在本地存储保存功能上,也将变量传递给 MySQL 数据库

python - 使用 MySQLdb 执行多个 SQL 查询

python - 如何选择列而不在 group by 子句中放置非聚合列

sql - SQL错误: A RETURN statement with a return value cannot be used in this context

mysql - 如何确保mysql列有4个字符

php - 如何将 HTML 输入字段的值插入 MySQL?

适用于 VS2008 的 MySqlConnector 6.9.9(框架 3.5)

mysql - 社交网站数据库管理

php - 在其他表中使用关系表的 ID

c# - 如何从 sql 多个表(具有外键的相关表)生成 c# 类