user130561
user130561

Reputation: 1533

SQL query - how to get counts in table

I have a table X that has column called version that has 4-5 values in it example 1,2,3,4,5

If the column value is 1 or 3 then I am good, else it's and error

question

what is the query so that I want output like this

Total # of values | Total good i.e. value is (1,3) | total failed i.e value not in (1,3)

Can someone please help me with the query

Upvotes: 0

Views: 108

Answers (1)

Taryn
Taryn

Reputation: 247670

You can try this:

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

Based on your comment, if you need the percentage you would use this:

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

Upvotes: 2

Related Questions