1

Bit of an SQL newbie question..

If I have a table along the lines of the following :

host    fault                                             fatal groupname
Host A  Data smells iffy                                  n     research
Host B  Flanklecrumpet needs a cuddle                     y     production
Host A  RAM loves EWE                                     n     research
Host Z  One of the crossbeams gone askew on the treadle   y     research

.. and I want to get some stats, I can..

select count(distinct host) as hosts, count(host) as faults, group from tablename group by groupname

.. which gives me the number of faults and affected hosts per groupname.

hosts    faults    groupname
2        3         research
1        1         production     

Can I, in the same query, show the number of fatal entries?

2 Answers 2

2

I would use aggregation, but in Postgres would phrase this as:

select groupname, count(distinct host) as hosts,
       count(*) as num_faults,
       count(*) filter (where fatal = 'Y') as num_fatal
from t
group by groupname;
Sign up to request clarification or add additional context in comments.

Comments

2

use conditional aggregation

 select count(distinct host) as hosts,
 count(host) as faults,sum(case when fatal='y' then 1 else 0 end) as numberofenty,
 groupname from tablename group by groupname

2 Comments

I fear one of us has misunderstood but I'm not sure which one :) I'd like the results to contain, per groupname, the number of rows where fatal == 'y'
Well, it did work but wasn't the solution I went for as the other one was, IMHO, cleaner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.