Select only one from multiple result

I find it’s hard to write the title for this topic, so here’s the problem.
I have a table like:

id | name  | status  | reg_date
---+-------+---------+-----------
1  | Jerry | online  | 2016-09-12
2  | Mary  | offline | 2016-09-02
3  | Rick  | online  | 2016-09-06
4  | Sarah | offline | 2016-09-13
5  | Donny | online  | 2016-09-01
6  | Naomi | online  | 2016-08-31
7  | Peter | online  | 2016-08-21

What I want to get is, to have 5 person order by reg_date for both status, but, I only want one person with ‘offline’ status. So, from the table I will have result like below:

id | name  | status  | reg_date
---+-------+---------+-----------
4  | Sarah | offline | 2016-09-13
1  | Jerry | online  | 2016-09-12
3  | Rick  | online  | 2016-09-06
5  | Donny | online  | 2016-09-01
6  | Naomi | online  | 2016-08-31

what if the offline person’s date comes before the latest 5 online dates?

what if the same person shows up twice in the latest 5 online dates?

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.