以下是我正在查询的数据库表的简化版本(我们称之为付款):
date | userid | payment
20/1/10 | 1 | 10
21/1/10 | 1 | 15
17/1/10 | 2 | 7
18/1/10 | 2 | 9
它记录用户在特定日期支付的款项。我需要找出每个用户首次付款的详细信息,如下所示:
20/1/10 | 1 | 10
17/1/10 | 2 | 7
存储过程是不可能的。有没有办法单独使用SQL,或者我只是在表中添加第一个付款标志?
答案 0 :(得分:5)
SELECT MIN([Date]), userid, payment
FROM Payments
GROUP BY Userid, payment
答案 1 :(得分:3)
SELECT MIN([Date]), UserID FROM Payments GROUP BY UserID
答案 2 :(得分:1)
试试这个:
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