mysql group by 计数_MySQL-带GROUP BY的条件计数

在MySQL中,作者遇到一个问题,需要在GROUP BY查询中只计算`droid_v`大于0的值。尝试了不同的SQL语句,最终找到解决方案:使用CASE WHEN语句结合COUNT(DISTINCT)来有条件地计数。正确查询应为:`SELECT puid, COUNT(DISTINCT CASE WHEN droid_v > 0 THEN droid_v END) AS droid, COUNT(DISTINCT sig_v) AS sig, SUM(NoExt) AS hits FROM temp GROUP BY puid`。" 40826103,4878285,Android通过JDBC连接SQL Server 2008实现数据库操作,"['Android开发', '数据库连接', 'JDBC驱动', 'SQL Server']
摘要由CSDN通过智能技术生成

bd96500e110b49cbb3cd949968f18be7.png

I'm trying to refine a query that I am currently using:

SELECT `puid`,

COUNT(DISTINCT `droid_v`) AS DROID,

COUNT(DISTINCT `sig_v`) AS sig,

SUM(NoExt) AS hits

FROM temp

GROUP BY `puid`

And I need to get it to only count droid_v where droid_V is greater than 0. Is it possible to condition the count in this way? At the moment, it is counting zero values as countable, and I can't really change the zeros to null values.

I do not need to know the count value for droid_V = 0, I only need to count it if it has a number greater than 0. That number will always be either 0, 1, 2, 3, or 4.

I have tried:

SELECT `puid`,

COUNT(DISTINCT CASE WHEN `droid_v`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值