如何计算满足条件的列中的所有值? (源码)

时间:2016-03-09 14:26:55

标签: sql sqlite count

我有一张表,其中包含该国所有病房中一组不同工作领域的工人数量。如何找到超过x工人数量的病房数量(跨所有工作领域)? 我当前的查询如下所示:

SELECT
  (SELECT COUNT(1) FROM Table WHERE
    (SELECT SUM(working) FROM Table GROUP BY table.ward) > x)
AS working FROM Table;

3 个答案:

答案 0 :(得分:2)

select count(ward) 
from
(SELECT ward FROM Table
 GROUP BY ward having SUM(working) > x) t

答案 1 :(得分:1)

SELECT ward, SUM(working) 
FROM YourTable 
GROUP BY ward
HAVING SUM(working) > @x

答案 2 :(得分:0)

SELECT COUNT(*)
FROM
(
    SELECT SUM(working) AS totalWorking
    FROM Table 
    GROUP BY table.ward
) workingPerWard
WHERE workingPerWard.totalWorking > x