我有一张包含以下信息的表格。
姓名状态
Test1运行
Test1运行
Test1运行
Test1运行
Test1 Down
Test2运行
Test2 Down
SQL Query应该返回结果,如
名称可用性
Test1 80%
Test2 50%
有什么建议吗?
答案 0 :(得分:2)
试试这个:
SELECT
Tests2.Name,
(Count(Name)* 100) / (SELECT Count(*) FROM Tests WHERE Tests.Name=Tests2.Name)
FROM Tests AS Tests2
WHERE Status='Running'
GROUP BY Name
Here是SQL Fiddle,我测试了它。
答案 1 :(得分:0)
select Name,
(sum(case when status='Running' then 1 else 0 end)/sum(case when 1=1 then 1 end))*100 as Availabilty
from table
group by Name