0
SELECT *
FROM UserLoginHistory 
WHERE loginDate BETWEEN '2022-10-03' AND '2022-10-09' 
GROUP BY userId,loginDate 
ORDER BY loginDate

How we can use LIMIT 0,5 Here so that we can LIMIT the records Date wise i.e 5 records per date

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • Welcome to Stackoverflow. Your question as it stands is very vague, you've given no indication of what is wrong with your current query, or what your expected results are. Please read the [How to ask a good question guide](https://stackoverflow.com/help/how-to-ask), and add additional details to your question so that it is clear exactly what you are trying to achieve, and why you have been unable to achieve it so far. Also, please [avoid uploading images of data, errors or code](https://stackoverflow.com/help/how-to-ask) and instead add the information as text. Thanks – GarethD Oct 10 '22 at 08:02
  • There are no aggregate functions so you should not be using group by. Also do you mean a maximum of 5 users per date? or a maximum of 5 dates per user. – P.Salmon Oct 10 '22 at 08:07
  • Perhaps https://stackoverflow.com/questions/12113699/get-top-n-records-for-each-group-of-grouped-results – P.Salmon Oct 10 '22 at 08:26

1 Answers1

0

I don't know if this is the right answer but I suggest you the following :

SELECT * FROM
  (SELECT *, RANK() OVER(PARTITION BY userId ORDER BY loginDate DESC) rang 
   FROM UserLoginHistory
   WHERE loginDate BETWEEN '2022-10-03' AND '2022-10-09'
   GROUP BY userId,loginDate
   ORDER BY loginDate) t
WHERE rang <= 5
Dadinaks
  • 93
  • 8