繁体   English   中英

SQL查询 - 如何获取表中的计数

[英]SQL query - how to get counts in table

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

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

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

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

有人可以帮我查询一下

你可以试试这个:

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 Fiddle DEMO

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM