0

我有一个表 X,其中有一个名为 version 的列,其中有 4-5 个值,例如 1,2,3,4,5

如果列值是 1 或 3,那么我很好,否则就是错误

问题

什么是查询,所以我想要这样的输出

值的总数 | 总好即价值是 (1,3) | 总失败即值不在 (1,3)

有人可以帮我查询吗

4

1 回答 1

2

你可以试试这个:

select count(*) as TotalValues
  , (select count(*) from test where id in(1, 3)) as TotalGood
  , (select count(*) from test where id not in (1, 3)) as TotalFailed
from test

SQL 小提琴演示

根据您的评论,如果您需要百分比,您将使用此:

SELECT TotalValues
  , TotalGood
  , TotalFailed
  , Cast(TotalGood as decimal(10, 2))/Cast(TotalValues as decimal(10, 2)) as PercentGood
FROM 
(
  select count(*) as TotalValues
    , (select count(*) from test where id in(1, 3)) as TotalGood
    , (select count(*) from test where id not in (1, 3)) as TotalFailed
  from test
) x
于 2012-05-11T19:52:30.823 回答