T-SQL去除重复行

例子:

CREATE TABLE [dbo].[testDT](
	[bh] [varchar](50) NULL,
	[name] [varchar](50) NULL,
	[gender] [int] NULL,
	[email] [varchar](50) NULL
) 
测试数据:
12 li 1 [email protected] 12 li 1 [email protected] 15 wang 0 [email protected] 13 zhang 1 [email protected] 13 zhang 1 [email protected] 

思路:

1. distinct筛选出来, 使用temp表来回倒。

2. 使用row_number() partition by 方法。如果重复行row_number会出现多个,例如1,2,3,删除row_number>1的行。

--sql 2005
select bh , name , sex , email from
(
  select t.* , row_number() over(partition by bh order by name , sex , email) px from testdt t
) m
where px = 1

3. group by 方法,其实相当于partition by的方法。

ref:https://bbs.csdn.net/topics/380198980?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

猜你喜欢

转载自www.cnblogs.com/watermarks/p/12573252.html