這是我當前的查詢:僅僅返回首行對每個加盟
SELECT
u.UserName, s.StatusName
FROM
UserStatus us
JOIN Status s on s.StatusId = us.StatusID
JOIN Users u on u.UserId = us.UserId
WHERE
us.UserId in (select UserId from Users)
AND us.DateCreated > '2017-07-14 00:00:00.000'
ORDER BY
us.DateCreated desc
它返回:
我怎麼會只得到最近的狀態,爲每個用戶?
編輯:
我不能只是做SELECT TOP 2
因爲我不知道有多少用戶存在將是你。有可能是500可能有10
這似乎是[如何選擇與MAX(列值)的行,DISTINCT由SQL中的另一列?](https://stackoverflow.com/questions/612231/how-can-i-select- row-with-maxcolumn-value-distinct-by-another-column-in-sql) –