0

I am including a SQLFiddle to show as an example of where I am currently at. In the example image you can see that simply grouping you get up to two lines per user depending on their status and how many of those statuses they have.

enter image description here

http://sqlfiddle.com/#!3/9aa649/2

The way I want it to come out is to look like the image below. Having a single line per user with two totaling columns one for Fail Total and one for Pass Total. I have been able to come close but since BOB only has Fails and not Passes this query leaves BOB out of the results. which I want to show BOB as well with his 6 Fail and 0 Pass

    select a.PersonID,a.Name,a.Totals as FailTotal,b.Totals as PassTotals from (
 select PersonID,Name,Status, COUNT(*) as Totals from UserReport
 where Status = 'Fail'
group by PersonID,Name,Status) a
join 
(
 select PersonID,Name,Status, COUNT(*) as Totals from UserReport
 where Status = 'Pass'
group by PersonID,Name,Status) b
on a.PersonID=b.PersonID

The below picture is what I want it to look like. Here is another SQL Fiddle that shows the above query in action http://sqlfiddle.com/#!3/9aa649/13

enter image description here

6
  • 1
    Possible duplicate of SQL Server PIVOT examples? Commented Oct 30, 2015 at 18:29
  • sorry i tag the wrong link technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx or this stackoverflow.com/questions/15931607/… Commented Oct 30, 2015 at 18:30
  • Wholly heck how the hell was this even answered so quickly. even if it was an easy answer it seemed like as soon as I hit submit 3 answers were already posted. Commented Oct 30, 2015 at 18:36
  • The thing is pivot question are asked a lot :). Commented Oct 30, 2015 at 18:37
  • @scripter78, when you will answer 5 such a question per day it becomes automatic. You see the question and your brain is not thinking any more. You are doing it in auto mode. This is the old standard trick in SQl. Commented Oct 30, 2015 at 18:40

3 Answers 3

2

Use conditional aggregation if the number of values for status column is fixed.

Fiddle

select PersonID,Name, 
sum(case when "status" = 'Fail' then 1 else 0 end) as failedtotal,
sum(case when "status" = 'Pass' then 1 else 0 end) as passedtotals 
from UserReport
group by PersonID,Name
Sign up to request clarification or add additional context in comments.

Comments

1

Use conditional aggregation:

select PersonID, Name,
       sum(case when Status = 'Fail' then 1 else 0 end) as FailedTotal,
       sum(case when Status = 'Pass' then 1 else 0 end) as PassedTotal
from UserReport
group by PersonID, Name;

1 Comment

Chose this one because it added the else 0 even though it was the second answer it is the most accurate to the question.
1

With conditional aggregation:

select PersonID, 
       Name,
       sum(case when Status = 'Fail' then 1 end) as Failed,
       sum(case when Status = 'Passed' then 1 end) as Passed
from UserReport
group by PersonID, Name

2 Comments

looks like you need else 0 other wise second column doesnt compute sqlfiddle.com/#!3/9aa649/25. the weird part is first one does compute ok.
Ok, that make more sense. And now I know why other answers include else 0 to avoid SUM( all null) sqlfiddle.com/#!3/9aa649/26.

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.