开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜