多条重复数据取第一条

使用分析函数row_number() over (partiion by ... order by ...)来进行分组编号,然后取分组标号值为1的记录即可。目前主流的数据库都有支持分析函数,很好用。

其中,partition by 是指定按哪些字段进行分组,这些字段值相同的记录将在一起编号;order by则是指定在同一组中进行编号时是按照怎样的顺序。

示例(SQL Server 2005或以上适用):

select s.*  
from ( 
    select *, row_number() over (partition by [手机号] order by [店铺]) as group_idx  
    from table_name
) s
where s.group_idx = 1 --------------------- 本文来自 KamChau 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/weixin_40439880/article/details/78190427?utm_source=copy

猜你喜欢

转载自blog.csdn.net/hzp666/article/details/82976952