Reputation: 21
Table
Member| Mum_Status
mem1 | active
mem2 | future
mem3 | active
mem3 | future
How to write a query to get below output from above table
Desired output:
Member| Status
mem1 | active
mem2 | future
mem3 | active
Upvotes: 2
Views: 46
Reputation: 44805
Do a GROUP BY
. Use MIN
to pick active (if available), otherwise future is returned.
select member, min(status)
from tablename
group by member
Will work very well as long as no other status types are introduced.
Alternatively, NOT EXISTS
:
select member, status
from tablename t1
where status = 'active'
or NOT EXISTS (select * from tablename t2
where t2.member = t1.member
and t1.status = 'active')
If a row has active status, return it. Otherwise it has future status, return it in case that no other row for same member has active status.
Upvotes: 5