sql 语句 去掉某些字段重复的行

表AA字段PUla123oob123ooc456ppd456pp我想只显示结果如下:就是只显示U字段和L字段相同的行PUla123ooc456pp求代码... 表AA
字段
P U l
a 123 oo
b 123 oo
c 456 pp
d 456 pp
我想只显示结果如下:就是只显示U字段和L字段相同的行
P U l
a 123 oo
c 456 pp

求代码
展开
 我来答
roy_88
2009-05-15 · TA获得超过3696个赞
知道大有可为答主
回答量:2914
采纳率:100%
帮助的人:2720万
展开全部
参照:
--取大小值
/******************************************************************************************************************************************************
Name相同ID最小或最大的记录

整理人:中国风(Roy)

日期:2008.06.06
******************************************************************************************************************************************************/

--> --> (Roy)生成测试数据

if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
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'
Go

--1、Name相同ID最小的记录(推荐用1,2,3),方法3在SQl05时,效率高于1、2
方法1:
Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID<a.ID)

方法2:
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

方法3:
select * from #T a where ID=(select min(ID) from #T where Name=a.Name)

方法4:
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

方法5:
select * from #T a group by ID,Name,Memo having ID=(select min(ID)from #T where Name=a.Name)

方法6:
select * from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)=0

方法7:
select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID)

方法8:
select * from #T a where ID!>all(select ID from #T where Name=a.Name)

方法9:
select * from #T a where ID in(select min(ID) from #T group by Name)

--SQL2005:

方法10:
select ID,Name,Memo from (select *,min(ID)over(partition by Name) as MinID from #T a)T where ID=MinID

生成结果:
/*
ID Name Memo
----------- ---- ----
1 A A1
4 B B1

(2 行受影响)
*/

--2、Name相同ID最大的记录,与min相反:
方法1:
Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID>a.ID)

方法2:
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

方法3:
select * from #T a where ID=(select max(ID) from #T where Name=a.Name) order by ID

方法4:
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

方法5:
select * from #T a group by ID,Name,Memo having ID=(select max(ID)from #T where Name=a.Name)

方法6:
select * from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)=0

方法7:
select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID desc)

方法8:
select * from #T a where ID!<all(select ID from #T where Name=a.Name)

方法9:
select * from #T a where ID in(select max(ID) from #T group by Name)

--SQL2005:

方法10:
select ID,Name,Memo from (select *,max(ID)over(partition by Name) as MinID from #T a)T where ID=MinID

生成结果2:
/*
ID Name Memo
----------- ---- ----
3 A A3
5 B B2

(2 行受影响)
*/
来自仙寓山能说会道的鲁肃
2009-05-15 · TA获得超过335个赞
知道小有建树答主
回答量:282
采纳率:0%
帮助的人:290万
展开全部
select min(P),U,I from AA group by U,I
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
dr850321
2009-05-15
知道答主
回答量:85
采纳率:0%
帮助的人:29.5万
展开全部
select distinct u,i from AA
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
tj_angela
2009-05-15 · TA获得超过7530个赞
知道大有可为答主
回答量:6797
采纳率:75%
帮助的人:2561万
展开全部
select p,u,i
from (select p,u,i,rank() over (partition by u,i order by p) rn
from AA) t
where rn=1
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式