I am using mysql. I don’t care how many groups get returned back but if a single group has more then 4 items i only want the first 4. How do i write a statement that only returns 4rows per group? As a temporary fix i am just returning them all and filtering it out in code. Its still pretty fast although it would be easier if i knew syntax
Share
If I understand your question correctly, I believe the following answer should do approximately what you need (note: I’ve included two test tables and their associated inserts as an example since your table structure was not provided):
Given these tables and data:
The following SQL will select purchase data by customer returning no more than the 4 most recent purchases:
Here is the resulting output from this select (note that Alex’s oldest transactions are absent):