COUNT()列按特定值

时间:2013-07-09 19:53:48

标签: sql sql-server-ce

我想在SQL Compact 4.0 DB-Table上进行查询,其中包含2个COUNT() - 列。第一列应计算所有行(COUNT(*)),第二列仅计算行,当特定列的十进制值高于或等于3.0时

我到目前为止:

SELECT COUNT(a.number) AS Participant, COUNT(b.specificColumn) AS Approved
FROM person AS a 
LEFT OUTER JOIN test AS b 
ON b.number = a.number

这样第二个COUNT()显然只计算实际有值的行!= NULL

2 个答案:

答案 0 :(得分:2)

我认为你不能用计数做到这一点。尝试使用案例陈述。未经测试:

SELECT COUNT(a.number) AS Participant, 
SUM(case when b.specificColumn >3 then 1 else 0 end) as Approved
FROM person AS a 
LEFT OUTER JOIN test AS b 
ON b.number = a.number

答案 1 :(得分:0)

SELECT COUNT(a.number) AS Participant,
       SUM(CASE WHEN b.specificColumn IS NULL THEN 0
                WHEN b.specificColumn >= 3 THEN 1
                ELSE 0) AS Approved
FROM person AS a 
LEFT OUTER JOIN test AS b 
ON b.number = a.number