0

im sure this is easy but im having a block I am trying to write some sql against a single table that has (simplified for example)

RunName, Result
foo,     pass
foo,     pass
foo,     fail 
foo,     pass
boo,     pass
boo,     fail
boo,     fail
soo,     pass

I was a query that will return a count of pass or fail for each name

something like for fail

foo, 1
boo, 2
soo, 0

and for pass

foo, 3
boo, 1
soo, 1
4
  • you need to revise your question, it is not clear what you want to know Commented Feb 28, 2012 at 18:32
  • Are you using MySQL or SQL Server? Please don't tag a question with two different flavors of RDBMS. Commented Feb 28, 2012 at 18:47
  • sorry it was mysql but we also use sql server for similar result storage so I added it as both. MySql working is fine for what I needed Commented Feb 28, 2012 at 18:55
  • Then it is still not a SQL Server question. Commented Feb 28, 2012 at 19:05

6 Answers 6

4

Normally you'd do a simple COUNT and GROUP BY RunName, but that will not show "zero results" like soo's fails.

This should work even for zero results (on MySQL)

SELECT RunName, SUM(Result='fail')
FROM TableA
GROUP BY RunName;

Demo here.

Edit: As Aaron points out, that only works on MySQL, this works on SQL Server also;

SELECT  RunName, SUM(CASE WHEN Result = 'fail' THEN 1 ELSE 0 END) as fails
FROM   TableA
GROUP BY RunName

Demo here.

Edit2: As Marcus points out, it's may not be a very index friendly way of doing the query, so if you have a primary key you may be better off doing a self join with a count/group to get the correct result;

SELECT a.RunName, COUNT(b.Result)
FROM TableA a LEFT JOIN TableA b ON a.id=b.id AND b.Result='fail'
GROUP BY RunName

Demo here.

Sign up to request clarification or add additional context in comments.

8 Comments

This will work in MySQL, but boolean expressions are not supported that way in SQL Server. I realize the question is tagged as both, so it is unclear.
@AaronBertrand Ah, only saw the MySQL part, I'll revise for both.
you know its a stupid question when you get 6 answers in minutes :) this worked as did some others thanks
Tag for SQL Server is SQL Server. SQL is for generic SQL.
@MarcusAdams The tag was changed, see discussion below the question.
|
2

This is for all the fail results, just change the where clause for pass:

select RunName, count(Result)
from [tableName]
where Result = 'fail'
group by RunName

Comments

1

You need to use a group by.

Something like this

Select count(result), runname, result from sometable
group by runname, result

this should give you output like

count runname result

2 foo pass

3 foo fail

Comments

1

Is this what you want?

SELECT  RunName, SUM(CASE WHEN Result = 'pass' THEN 1 ELSE 0 END) as NumPass,
        SUM(CASE WHEN Result = 'fail' THEN 1 ELSE 0 END) as NumFail
FROM   yourtable
GROUP BY RunName

If you want separate results for each then you can just do a filter on the where clause like so:

    SELECT  RunName, COUNT(1) as NumPass
    FROM    yourtable
    WHERE   Result = 'pass'
GROUP BY RunName


SELECT  RunName, COUNT(1) as NumFail
FROM    yourtable
WHERE   Result = 'fail'
GROUP BY RunName

Comments

1

Try this for fail

select RunName, count(Result) as cnt
from your_table
where Result = 'fail'
group by RunName

and this for pass

select RunName, count(Result) as cnt
from your_table
where Result = 'pass'
group by RunName

Comments

0

Try this:

For Pass:

SELECT RunName, COUNT(*)
FROM TableName
GROUP BY Result
WHERE UPPER(Result) = 'PASS'

For Fail:

SELECT RunName, COUNT(*)
FROM TableName
GROUP BY Result
WHERE UPPER(Result) = 'FAIL'

6 Comments

UPPER() isn't necessary here. It causes an extra filesort for the GROUP BY, rather than using an index on Result.
@MarcusAdams First of all you are ASSuming that the OP is using SQL Server, and Second of all not all RDBS are case agnostic (ie Oracle). I develop software that runs against 10 different databases so I write my SQL queries to run against all databases.
Use GROUP BY and WHERE Result = 'pass'. I wouldn't assume, given the data provided by the OP, that data will include 'PASS' and 'pass'. I was offering criticism on using a function in GROUP BY and didn't even notice you don't have a WHERE clause. -2 for scanning all rows.
@MarcusAdams - Ok, now I see what you were talking about originally, I just edited my code.
If you improve your query, or at least explain your reasoning for assuming the column will be mixed case, I will remove the down vote. Don't take it personally, dude. Even with the WHERE clause, you're still scanning all rows unless you remove the UPPER() function, but at least you tried.
|

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.