sqlserver分组后取一条默认数据

select * from TestUsera;

id  name  phone

1 张三 13427219158
2 张三 13827826495
3 李四 15849543155
4 王五 15884562583

 

按姓名分组后 绑定个默认手机号

select b.name,c.phone
from
(SELECT name FROM TestUsera GROUP BY name ) b
left join
(
select a.*, ROW_NUMBER() OVER(partition by name order by phone  desc) as nums from TestUsera a
) c
on b.name=c.name where c.nums=1;

 

 

 

采用 ROW_NUMBER() OVER()方法

按name为key ,phone 排序获取默认:

ROW_NUMBER() OVER(partition by name order by phone ) as nums 

 

posted @ 2022-10-16 11:54  .L  阅读(124)  评论(0编辑  收藏  举报