【sql基礎】按照名字分組查詢時間最先的一條記錄

給出2種解決方案spa

 

rownumbercode

SELECT *
FROM
(
SELECT IdentityID, OpenID, ROW_NUMBER() OVER(PARTITION BY OpenID ORDER BY CreateTime DESC
) AS rownumber FROM dbo.T_Account
) AS tmp
WHERE tmp.rownumber = 1

 

 

相關子查詢blog

SELECT DISTINCT OpenID, test1.IdentityID FROM dbo.T_Account 
AS test1
WHERE test1.IdentityID
IN 
(
SELECT TOP 1 IdentityID FROM dbo.T_Account
WHERE dbo.T_Account.OpenID =  test1.OpenID
ORDER BY CreateTime DESC

)

 

 

查詢結果it

 

相關文章
相關標籤/搜索