Reputation: 1112
Trying to work out how best to phrase this questions, but best done by example I think.
I have the following output from a query
league_id user_id outcome_id
5 1 1
5 1 4
5 2 1
As you can see there are two different outcome values. It is valid that there would be more than one case of league_id and user_id being the same… eg in the example above 5 and 1.
What I'd like is to produce summary data whereby if a combination of league_id and user_id has an outcome of '4' then there would be output of 'FAIL' and all the outcomes of '1' are ignored for that league_id and user_id combinations. If there is no presence of a '4' then the summary would output pass.
It would result in something like below…
league_id user_id outcome_id
5 1 FAIL
5 2 PASS
Sorry to say I'm stumped how I would achieve this! Can someone help please?
Thanks.
DS
Upvotes: 0
Views: 859
Reputation: 28741
SELECT league_id,user_id,
CASE WHEN outcome=0 THEN 'PASS' ELSE 'FAIL' END as outcome_id
FROM
(
SELECT league_id,user_id,SUM(CASE WHEN outcome_id=4 THEN 1 ELSE 0 END) as outcome
FROM tableName
GROUP BY league_id,user_id
) As Z
Upvotes: 2