我想做以下事情:
1)查找表格中的总行数 2)找到符合特定条件的总行数。
3)从(2)中减去(1)。
示例表员工:
EmployeeID Nationality
1 Brazil
2 Korea
3 Germany
4 Brazil
5 Brazil
我尝试了什么:
SELECT count(EmployeeID) as Total from Employees
UNION
SELECT count(EmployeeID) as Brazilians from Employees
WHERE Nationality = 'Brazil'
结果:
Total
5
3
第1行将为我提供员工总数。第2行将给我巴西员工。 我使用UNION来查看是否可以从第1行中减去第2行。
我可以使用CASE和SUM()执行此操作,但这需要row_number()函数,由于我正在使用WebSQL,因此无法使用该函数。是否有另一种方法可以索引这些行以便能够减去?
我可以使用另一种方法来解决这个看似简单的问题吗?
答案 0 :(得分:5)
如何计算不符合该标准的行?
SELECT COUNT(EmployeedID) as non_brazilians
FROM Employees
WHERE Nationality <> 'Brazil';
答案 1 :(得分:1)
您可以使用条件聚合:
Dim result As Boolean
InternetConnection flags;
result = InternetGetConnectedState( flags, 0 );
If result = False Then
' TODO: Call GetLastError to eliminate other causes of failure before determining it is indeed due to a lack of an internet connection
End If
这假定select count(*) as TotalRows,
sum(case when Nationality = 'Brazil' then 1 else 0 end) as Brazilians,
sum(case when Nationality <> 'Brazil' then 1 else 0 end) as nonBrazilians
from Employee;
永远不会Nationality
。如果可能,最后一个条件应该是:
NULL
答案 2 :(得分:0)
试试这个:
SELECT count(*) AS TotalRows
, (SELECT count(EmployeeID) FROM WHERE Nationality = 'Brazil') as Brazilians
, (count(*) - (SELECT count(EmployeeID) FROM WHERE Nationality = 'Brazil')) AS Subtract1From2
FROM Employee