我是SQL的新手,我正在尝试针对SQL Server CE 4数据库构建SQL查询,该数据库基于其CompanyName
状态(0或0)多次计算同一列(Active
) 1)。我需要按状态计算的Total,Active和In Active。我认为这是Total,Active和In Active的子查询,但我不确定如何构建它。
基本上我想要一个基于CompanyName
提供这样结果的表。
State Total Active InActive
Texas 10 6 4
Florida 15 5 10
我到目前为止的基本查询是
SELECT State, COUNT(CompanyName) AS Total
FROM JobApps AS j
GROUP BY State
ORDER BY Total DESC
感谢任何帮助。
答案 0 :(得分:3)
SUM
使用CASE
语句在同一分组上:
SELECT State, COUNT(CompanyName) AS Total
, SUM(CASE Active WHEN 1 THEN 1 ELSE 0 END) AS Active
, SUM(CASE Active WHEN 1 THEN 0 ELSE 1 END) AS InActive
FROM JobApps AS j
GROUP BY State
ORDER BY Total DESC