【sql基础】按照名字分组查询时间最早的一条记录

给出2种解决方案

 

rownumber

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

 

相关子查询

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
)

 

查询结果

【sql基础】按照名字分组查询时间最早的一条记录

 

上一篇:在sqlserver中如何从字符串中提取数字,英文,中文,过滤重复字符


下一篇:sqlserver重组索引,优化碎片