简体   繁体   中英

mysql - ignore other records if a condition is met

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM