如何将这两个计数函数放入一个查询中?
SELECT
COUNT(MaritalStatus) as 'Married'
FROM
Person.Person PP
INNER JOIN
HumanResources.Employee HRE ON PP.BusinessEntityID = HRE.BusinessEntityID
WHERE
MaritalStatus = 'M';
SELECT
COUNT(MaritalStatus) as 'Single'
FROM
Person.Person PP
INNER JOIN
HumanResources.Employee HRE ON PP.BusinessEntityID = HRE.BusinessEntityID
WHERE
MaritalStatus = 'S';
最佳答案
您可以在以下情况下尝试使用总和大小写:
SELECT SUM(CASE WHEN MaritalStatus = 'M' THEN 1 ELSE 0 END),
SUM(CASE WHEN MaritalStatus = 'S' THEN 1 ELSE 0 END)
FROM Person.Person PP INNER JOIN HumanResources.Employee HRE
ON PP.BusinessEntityID = HRE.BusinessEntityID
关于SQL:单个查询中有两个计数函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40922612/