COUNT DISTINCT 如果总和为零

COUNT DISTINCT if sum is zero

如果 B 列的总和等于 0 按 A 列分组,则需要帮助计算 A 列中的值。

SELECT    
A, SUM(B) --???   
FROM table  
WHERE DATE = 'yyyy-mm-dd'   
AND B = 0  
GROUP BY A 

总和(B)=0 的 A 的数量。

select count(*)A
from (
    SELECT A
    FROM table  
    WHERE DATE = ... -- your date here
    GROUP BY A 
    HAVING SUM(B)=0
) t
select count(Distinct A) 
from (
    SELECT sum(B) as B, A
    FROM table  
    GROUP BY A 
    ) a
 Where B = 0 and DATE = 'yyyy-mm-dd'   

现在可以了。谢谢大家分享解决方案。

select count(*) MACHINE_CODE  
from (
    SELECT MACHINE_CODE  
    FROM CNC_hourly_logging chl  
    WHERE C_DATE ='2022-01-28'  
    AND cast(convert(char(8), [C_TIME], 108) as time) BETWEEN cast('08:00' as time) AND cast('18:30' as time)  
    GROUP BY MACHINE_CODE  
    HAVING SUM(TOTAL_PRODUCTION)=0  
) t