Need help with SQL query
The following is a simplified version of a database table I'm querying (let's call it Payments):
date | userid | payment
20/1/10 | 1 | 10
21/1/10 | 1 | 15
17/1/10 | 2 | 7
18/1/10 | 2 | 9
It records payments made by users on certain dates. I need to find out the details of the first pa开发者_如何学Pythonyment made by each user like so:
20/1/10 | 1 | 10
17/1/10 | 2 | 7
Stored procedures are out of the question. Is there any way to do this using SQL alone or should I just add a first payment flag to the table?
SELECT MIN([Date]), userid, payment
FROM Payments
GROUP BY Userid, payment
SELECT MIN([Date]), UserID FROM Payments GROUP BY UserID
Try this:
SELECT * FROM Payments
INNER JOIN (SELECT Min([Date]) AS MinDate, UserID
FROM Payments GROUP BY UserID) AS M
ON M.MinDate = Payments.Date AND M.UserID = Payments.UserID
精彩评论