sql server 不同sql数据库库 表连查

查看: 1971|回复: 3
sql语句去除重复记录(多表连接的查询)
TA的每日心情开心5&天前签到天数: 2 天[LV.1]初来乍到
--处理表重复记录(查询和删除)
/******************************************************************************************************************************************************
1、Num、Name相同的重复值记录,没有大小关系只保留一条
2、Name相同,ID有大小关系时,保留大或小其中一个记录
******************************************************************************************************************************************************/
--1、用于查询重复处理记录(如果列没有大小关系时2000用生成自增列和临时表处理,SQL2005用row_number函数处理)
--& --& 生成測試數據
if not object_id('Tempdb..#T') is null
& & drop table #T
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
--I、Name相同ID最小的记录(推荐用1,2,3),方法3在SQl05时,效率高于1、2
Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID&a.ID)
select a.* from #T a join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID
select * from #T a where ID=(select min(ID) from #T where Name=a.Name)
select a.* from #T a join #T b on a.Name=b.Name and a.ID&=b.ID group by a.ID,a.Name,a.Memo having count(1)=1
select * from #T a group by ID,Name,Memo having ID=(select min(ID)from #T where Name=a.Name)
select * from #T a where (select count(1) from #T where Name=a.Name and ID&a.ID)=0
select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID)
select * from #T a where ID!&all(select ID from #T where Name=a.Name)
方法9(注:ID为唯一时可用):
select * from #T a where ID in(select min(ID) from #T group by Name)
--SQL2005:
select ID,Name,Memo from (select *,min(ID)over(partition by Name) as MinID from #T a)T where ID=MinID
select ID,Name,Memo from (select *,row_number()over(partition by Name order by ID) as MinID from #T a)T where MinID=1
ID& && && & Name Memo
----------- ---- ----
1& && && &&&A& & A1
4& && && &&&B& & B1
(2 行受影响)
--II、Name相同ID最大的记录,与min相反:
Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID&a.ID)
select a.* from #T a join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID order by ID
select * from #T a where ID=(select max(ID) from #T where Name=a.Name) order by ID
select a.* from #T a join #T b on a.Name=b.Name and a.ID&=b.ID group by a.ID,a.Name,a.Memo having count(1)=1
select * from #T a group by ID,Name,Memo having ID=(select max(ID)from #T where Name=a.Name)
select * from #T a where (select count(1) from #T where Name=a.Name and ID&a.ID)=0
select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID desc)
select * from #T a where ID!&all(select ID from #T where Name=a.Name)
方法9(注:ID为唯一时可用):
select * from #T a where ID in(select max(ID) from #T group by Name)
--SQL2005:
select ID,Name,Memo from (select *,max(ID)over(partition by Name) as MinID from #T a)T where ID=MinID
select ID,Name,Memo from (select *,row_number()over(partition by Name order by ID desc) as MinID from #T a)T where MinID=1
生成结果2:
ID& && && & Name Memo
----------- ---- ----
3& && && &&&A& & A3
5& && && &&&B& & B2
(2 行受影响)
--2、删除重复记录有大小关系时,保留大或小其中一个记录
--& --& 生成測試數據
if not object_id('Tempdb..#T') is null
& & drop table #T
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
--I、Name相同ID最小的记录(推荐用1,2,3),保留最小一条
delete a from #T a where&&exists(select 1 from #T where Name=a.Name and ID&a.ID)
delete a&&from #T a left join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)
方法4(注:ID为唯一时可用):
delete a from #T a where ID not in(select min(ID)from #T group by Name)
delete a from #T a where (select count(1) from #T where Name=a.Name and ID&a.ID)&0
delete a from #T a where ID&&(select top 1 ID from #T where Name=a.name order by ID)
delete a from #T a where ID&any(select ID from #T where Name=a.Name)
select * from #T
ID& && && & Name Memo
----------- ---- ----
1& && && &&&A& & A1
4& && && &&&B& & B1
(2 行受影响)
--II、Name相同ID保留最大的一条记录:
delete a from #T a where&&exists(select 1 from #T where Name=a.Name and ID&a.ID)
delete a&&from #T a left join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
delete a from #T a where ID not in (select max(ID) from #T where Name=a.Name)
方法4(注:ID为唯一时可用):
delete a from #T a where ID not in(select max(ID)from #T group by Name)
delete a from #T a where (select count(1) from #T where Name=a.Name and ID&a.ID)&0
delete a from #T a where ID&&(select top 1 ID from #T where Name=a.name order by ID desc)
delete a from #T a where ID&any(select ID from #T where Name=a.Name)
select * from #T
ID& && && & Name Memo
----------- ---- ----
3& && && &&&A& & A3
5& && && &&&B& & B2
(2 行受影响)
--3、删除重复记录没有大小关系时,处理重复值
--& --& 生成測試數據
if not object_id('Tempdb..#T') is null
& & drop table #T
Create table #T([Num] int,[Name] nvarchar(1))
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'
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
alter table #T add ID int identity--新增标识列
delete a from&&#T a where&&exists(select 1 from #T where Num=a.Num and Name=a.Name and ID&a.ID)--只保留一条记录
alter table #T drop column ID--删除标识列
--查看结果
select * from #T
Num& && && &Name
----------- ----
1& && && &&&A
2& && && &&&B
(2 行受影响)
--重新执行测试数据后用方法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
& & set rowcount @
& & delete #T where Num=@Num and Name=@Name
& & set rowcount 0;
& & fetch next from Roy_Cursor into @con,@Num,@Name
close Roy_Cursor
deallocate Roy_Cursor
--查看结果
select * from #T
Num& && && &Name
----------- ----
1& && && &&&A
2& && && &&&B
(2 行受影响)
TA的每日心情开心 20:22签到天数: 2 天[LV.1]初来乍到
感谢楼主分享呦。
TA的每日心情郁闷 13:55签到天数: 4 天[LV.2]偶尔看看I
恩恩,不错,还有那么多种方法呀
TA的每日心情开心 15:55签到天数: 1 天[LV.1]初来乍到
重复数据去除,有时候真的很需要的。
Beijing Aptech Beida Jade Bird Information Technology Co.,Ltd
北大青鸟IT教育 北京阿博泰克北大青鸟信息技术有限公司 版权所有跨数据库连表查询sql语句怎么写?_百度知道
跨数据库连表查询sql语句怎么写?
那么我可以通过A数据库EMP表的fdepartmentid查询出部门名称departname.,我这是A数据库查询职员信息EMP表:select fdepartmentid,请问这sql语句该怎么写,这是我的sql语句,求大神指导,而这个fdepartmentid我是想显示为另外一个B数据库dept表的departid的值各位早上好,请教一个sql语句的问题,然后通过departname查询出B数据库depart表的departid, from A
b。 select a,这个部门名称在A库上也有了吧.fdepartmentid
join B,假设部门名称存在A库的表T_DEPT中.fdepartmentid.T_EMP a join A通过fdepartmentid查询出部门名称departname.,c..fdepartmentid = b.depart c on b.departname = c.T_DEPT b on a.departidfrom A.departname.
这样是可以查询出B数据库的departid,不过只有一条数据,你能不能远程给我看一下?
因为都是一一对应的,所以只有一条。 select a.fdepartmentid,b.departname,c.departidfrom A..T_EMP a left join A..T_DEPT b on a.fdepartmentid = b.fdepartmentid
left join B..depart c on b.departname = c.departname 这样,可以查出所有的员工和有对应的deptid
来自团队:
其他类似问题
为您推荐:
其他2条回答
样应该符合你要求,departname from A;SQLOLEDB&#39,',' Password=数据库B密码&#39:opendatasource(&#39.deptwhere fdepartmentid = departid---或者写你需要的条件 远程数据库操作.数据库;Data Source=数据库B的IP.;User ID=sa:SQLOLEDB'User ID= Password=数据库B密码&#39,opendatasource(&#39.;;Data Source=数据库B的IP.;);).数据库.T_EMP
不能直接查询出departname的,它是在depart表里面,首先查询出fdepartmentid作为条件再查询出departname作为条件查询出B数据库的depart表的departid
一般ID是唯一而且对应的才对的。select fdepartmentid , b.departid from A..T_EMP,A..T_DEPT as a,opendatasource('SQLOLEDB','Data Source=数据库B的IP;User ID= Password=数据库B密码').数据库..dept as bwhere fdepartmentid = a.departidand a.departname = b.departname
SELECT * FROM
SBOFL..table1 LEFT JOIN SBOFK..TABLE2 ON ...SBOFL和SBOFK代表两个数据库
sql语句的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁SQLSERVER跨数据库表联合查询 - gskcc - 博客园
数据库1:dbA;数据库1中表1:tb1
数据库2:dbB;数据库2中表2:tb2
联合查询这两个表
select * from dbA.dbo.tb1 t1
left join dbB.dbo.tb2 t2 on t1.key1=t2.key2
关键:表明的前缀:dbA.dbo.tb1,即:数据库名.dbo.表名君,已阅读到文档的结尾了呢~~
SQL数据库查询语句连接查询多表连接查询 SQL数据库查询语句连接查询多表连接查询
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
SQL数据库查询语句连接查询多表连接查询
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口SQLSERVER 表 自身连接查询问题_百度知道
SQLSERVER 表 自身连接查询问题
c 但是出来了很多重复的行,我想知道正确的方式怎么写,例如反复出现第二行,现在第四和第五行出错了.a=y.b&y:A
10B和C都应该是依次增大的值;y.c&lt.b and x.a and x表M如下,M as y where x,我想找出来我是这样写的select *
from M as x,我的方法为什么错了
提问者采纳
#tem b where b,1?可否这么用Select identity(int!=1).a and (b!=1 or b,1) id,1) id.id=1 anda;b.a=b,#tem b where a.* from #tem a.b&gt.id-a,* into #tem from Mselect a.c.b-a.a and a.c-a.b
依次增大的意思是后一行应该比前一行大1吧.* from #tem
a.id=b,* into #tem from M select b.b.id-1 and aSelect identity(int.a=b,1
a.id=b.id-1 是我思考上的盲区,谢谢
提问者评价
其他类似问题
4人觉得有用
为您推荐:
其他1条回答
&gt.c&lt.B-m,并且你是2005以上的版本的话select * from (select *,row_number() over(order by C)rn from m )a where m,如果C的排序正确正确的方法;5你的错误在于你只分析了错误数据符合你的条件
非常感谢你提供了我一个新的函数,可惜分我只能给一个人
呵呵,对你有帮助我的回答就没有浪费
连接查询的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 sql数据库 的文章

 

随机推荐