查询有重复的数据(没有重复的也显示)只显示一条,字段NAME,CITY,都相同的取icount最小的

hainan 2010-07-23 08:18:18
表字段 ID,name, city, icount

查询有重复的数据(没有重复的也显示)只显示一条,字段NAME,CITY,都相同的取icount最小的
可以是SQL2005
谢谢
...全文
78 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
永生天地 2010-07-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xman_78tom 的回复:]
SQL code

select id,name,city,icount from (
select *, row_number() over (partition by name,city order by icount) rn from tab) t
where rn=1
[/Quote]
难道这样的速度最快
rart2008 2010-07-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wufeng4552 的回复:]

SQL code
--进来了就参与一下
select *
from tb t
where icount=(select min(icount)
from tb
where NAME=t.NAME and city=t.city)
[/Quote]
5楼正解!
水族杰纶 2010-07-23
  • 打赏
  • 举报
回复
--进来了就参与一下
select *
from tb t
where icount=(select min(icount)
from tb
where NAME=t.NAME and city=t.city)
心中的彩虹 2010-07-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 hainan 的回复:]
表字段 ID,name, city, icount

查询有重复的数据(没有重复的也显示)只显示一条,字段NAME,CITY,都相同的取icount最小的
可以是SQL2005
谢谢
[/Quote]


select ID,name,city,icount from tb a where not exists(select 1 from tb where a.name=name and a.city=city a.icount>icount)


--2 row_number() over是2005 新增的分析函数 很好
select ID,name,city,icount
from
(select ID,name,city,icount,
row_number() over(partition by name,city order by icount) rk
from tb) a
where a.rn=1


xman_78tom 2010-07-23
  • 打赏
  • 举报
回复

select id,name,city,icount from (
select *, row_number() over (partition by name,city order by icount) rn from tab) t
where rn=1
bancxc 2010-07-23
  • 打赏
  • 举报
回复
select * from tb t
where not exists (
select 1 from tb where t.name=name and t.city=city and t.icount>icount
)
华夏小卒 2010-07-23
  • 打赏
  • 举报
回复
select * from tb t
where (select count(*) from tb where NAME=t.NAME and CITYCITY=t.CITYCITY and t.icount<icount)<1

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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