SQLServer查询去重

第一种: Distinct

 SELECT Distinct列名称 FROM 表名称

e.g. 

 

 SELECT Distinct userid FROM UserInfo;

但是Distinct只能放在字段的开头,如果多个字段,将不会去重。

SELECT Distinct userid, id FROM UserInfo;

 

第二种,row_number() over() 函数

SELECT
 top 10
  *
FROM
(
SELECT
     *
FROM
    (
    SELECT
        a,b,c,d,
        ROW_NUMBER () OVER ( Partition BY a ORDER BY b ) AS row 
    FROM
        tab_name1
    ) p 
WHERE
p.row =1
) as aa
LEFT JOIN tab_name2 b ON aa.xx = b.xx

 

参考:

https://blog.csdn.net/qq_36330228/article/details/84174817

posted @ 2020-02-28 16:07  sevck  阅读(1510)  评论(0编辑  收藏