I need to write a query where I need to retrieve the data from a table.
Table
===================================
ID | userID | Status | date |
===================================
1 3333 Queued xxxx
2 4444 Queued yyyy
3 5555 Finished zzzzz
5 6666 Queued iiiii
6 7777 Queued kkkkk
Now i want to retrieve the row only if the status=”Queued” and rows with status= “Queued” are more than 2 and the row with most recent ID . ie i want the answer to be ID = 6
I tried with the below query
select * from t1 where status = "Queued" GROUP BY status HAVING count(status) > 2 ORDER BY ID DESC limit 1
You could use a subquery for that. The problem is that the ordering is done before the grouping. Something like this should work: