关于SQL去除重复项的问题

英文字母打字员 2011-11-11 11:27:38
模拟数据如下:



国家单位 1,1,1,1 1 1 1 张三 11
国家单位 1,1,1,1 1 1 1 李四 11
北京市单位 1 1 1 1 李四 11
各省、自治区、直辖市和计划单列市 1 1 1 1 张三 11
各省、自治区、直辖市和计划单列市 1 1 1 1 李四 11
dddddd 1 1 1 1 1 1 1 1 张三 11
dddddd 1 1 1 1 1 1 1 点点滴滴 11
ddddddddddd 1 1 1 1 1 1 1 1 张三 11
ddddddddddd 1 1 1 1 1 1 1 1 李四 11
ddddddddddd 1 1 1 1 1 1 1 李四 11




求去除重复的机构信息的SQL语句
...全文
158 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-11-11
  • 打赏
  • 举报
回复
select
case px when 1 then col1 else '' end as col1,
case px when 1 then col2 else '' end as col2,
*
from
(select px=row_Number()over(partition by col1,col2 order by getdate()),* from tb)t
--小F-- 2011-11-11
  • 打赏
  • 举报
回复
select
case px when 1 then col1 else '' end as col1,
case px when 1 then col2 else '' end as col2,
*
from
(select px=row_Number()over(parition by col1,col2 order by getdate()),* from tb)t
--小F-- 2011-11-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wanglejun 的回复:]
引用 1 楼 orchidcat 的回复:
给出你要的结果。


机构信息只出现一次。

比如

国家单位 1,1,1,1 1 1 1 张三 11
国家单位 1,1,1,1 1 1 1 李四 11

SQL以后就是等于
模拟数据如下:

SQL code

国家单位 1,1,1,1 1 1 1 张三 11
1 1 1 李四 11

这样子的2条记……
[/Quote]

晕 早说啊。
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 beirut 的回复:]
SQL code
--2005 以及以上版本
with t as
(
select row_number() over(partition by 字段 order by 字段) as new_id ,* from tb
)
delete from t where new_id>1
[/Quote]

select dense_RANK() over(partition by Orgname order by Orgname desc),Orgname, Personname from Planuser; 不对啊
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 orchidcat 的回复:]
给出你要的结果。
[/Quote]

机构信息只出现一次。

比如

国家单位 1,1,1,1 1 1 1 张三 11
国家单位 1,1,1,1 1 1 1 李四 11

SQL以后就是等于
模拟数据如下:

SQL code

国家单位 1,1,1,1 1 1 1 张三 11
1 1 1 李四 11

这样子的2条记录
黄_瓜 2011-11-11
  • 打赏
  • 举报
回复
--2005 以及以上版本
with t as
(
select row_number() over(partition by 字段 order by 字段) as new_id ,* from tb
)
delete from t where new_id>1
--小F-- 2011-11-11
  • 打赏
  • 举报
回复
if object_id('Tempdb..#') is not null
drop table #
Select distinct * into # from #T--排除重复记录结果集生成临时表#

truncate table #T--清空表

insert into #T select * from # --把临时表#插入到表#T中

--查看结果
select * from #T

--小F-- 2011-11-11
  • 打赏
  • 举报
回复
删除重复记录没有大小关系时,处理重复值


--> --> (Roy)生成測試數據

if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([Num] int,[Name] nvarchar(1))
Insert #T
select 1,N'A' union all
select 1,N'A' union all
select 1,N'A' union all
select 2,N'B' union all
select 2,N'B'
Go

方法1:
if object_id('Tempdb..#') is not null
drop table #
Select distinct * into # from #T--排除重复记录结果集生成临时表#

truncate table #T--清空表

insert #T select * from # --把临时表#插入到表#T中

--查看结果
select * from #T

/*
Num Name
----------- ----
1 A
2 B

(2 行受影响)
*/

--重新执行测试数据后用方法2
方法2:

alter table #T add ID int identity--新增标识列
go
delete a from #T a where exists(select 1 from #T where Num=a.Num and Name=a.Name and ID>a.ID)--只保留一条记录
go
alter table #T drop column ID--删除标识列

--查看结果
select * from #T

/*
Num Name
----------- ----
1 A
2 B

(2 行受影响)

*/

--重新执行测试数据后用方法3
方法3:
declare Roy_Cursor cursor local for
select count(1)-1,Num,Name from #T group by Num,Name having count(1)>1
declare @con int,@Num int,@Name nvarchar(1)
open Roy_Cursor
fetch next from Roy_Cursor into @con,@Num,@Name
while @@Fetch_status=0
begin
set rowcount @con;
delete #T where Num=@Num and Name=@Name
set rowcount 0;
fetch next from Roy_Cursor into @con,@Num,@Name
end
close Roy_Cursor
deallocate Roy_Cursor

--查看结果
select * from #T
/*
Num Name
----------- ----
1 A
2 B

(2 行受影响)
*/
Mr_Nice 2011-11-11
  • 打赏
  • 举报
回复
给出你要的结果。

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧