Reputation:
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
Upvotes: 3
Views: 232
Reputation: 530
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:
DROP TABLE IF EXISTS tCustomer;
CREATE TABLE tCustomer (
customerId INT(11) UNSIGNED NOT NULL auto_increment,
name VARCHAR(8),
PRIMARY KEY (customerId)
) AUTO_INCREMENT=1;
INSERT INTO tCustomer VALUES
(NULL, 'Alex'),
(NULL, 'Bob'),
(NULL, 'Carl')
;
DROP TABLE IF EXISTS tPurchases;
CREATE TABLE tPurchases (
purchaseId INT(11) UNSIGNED NOT NULL auto_increment,
customerId INT(11) UNSIGNED NOT NULL,
amount DECIMAL(9,2),
purchaseDate DATETIME,
PRIMARY KEY (purchaseId),
CONSTRAINT fk_customer FOREIGN KEY (customerId) REFERENCES tCustomer (customerId)
) AUTO_INCREMENT=1;
INSERT INTO tPurchases VALUES
(NULL, 1, 1.00, '2011-01-01 08:00'),
(NULL, 1, 1.01, '2011-01-02 08:00'),
(NULL, 1, 1.02, '2011-01-03 08:00'),
(NULL, 1, 1.03, '2011-01-04 08:00'),
(NULL, 1, 1.04, '2011-01-05 08:00'),
(NULL, 1, 1.05, '2011-01-06 08:00'),
(NULL, 2, 1.01, '2011-01-01 08:00'),
(NULL, 2, 1.02, '2011-01-02 08:00'),
(NULL, 3, 1.01, '2011-01-02 08:00'),
(NULL, 3, 1.02, '2011-01-04 08:00'),
(NULL, 3, 1.03, '2011-01-08 08:00')
;
The following SQL will select purchase data by customer returning no more than the 4 most recent purchases:
SELECT
tC.customerId, tC.name, tP.purchaseDate, tP.amount, COUNT(tPNewer.customerId) newerCt
FROM
tCustomer tC
INNER JOIN tPurchases tP ON tC.customerId = tP.customerId
LEFT OUTER JOIN tPurchases tPNewer ON tP.customerId = tPNewer.customerId AND tPNewer.purchaseId > tP.purchaseId
GROUP BY
tC.customerId,
tC.name,
tP.purchaseDate,
tP.amount
HAVING
newerCt < 4 -- Ignore rows that have more than 3 newer records
ORDER BY
tC.customerId,
tP.purchaseDate desc
;
Here is the resulting output from this select (note that Alex's oldest transactions are absent):
+------------+------+---------------------+--------+---------+
| customerId | name | purchaseDate | amount | newerCt |
+------------+------+---------------------+--------+---------+
| 1 | Alex | 2011-01-06 08:00:00 | 1.05 | 0 |
| 1 | Alex | 2011-01-05 08:00:00 | 1.04 | 1 |
| 1 | Alex | 2011-01-04 08:00:00 | 1.03 | 2 |
| 1 | Alex | 2011-01-03 08:00:00 | 1.02 | 3 |
| 2 | Bob | 2011-01-02 08:00:00 | 1.02 | 0 |
| 2 | Bob | 2011-01-01 08:00:00 | 1.01 | 1 |
| 3 | Carl | 2011-01-08 08:00:00 | 1.03 | 0 |
| 3 | Carl | 2011-01-04 08:00:00 | 1.02 | 1 |
| 3 | Carl | 2011-01-02 08:00:00 | 1.01 | 2 |
+------------+------+---------------------+--------+---------+
Upvotes: 2