db2 顺序排列问题

ww521bmw 2010-10-21 09:28:00
数据:
北京地区 aa 20
天津地区 aa 30
北京地区 bb 10
天津地区 bb 40


结果,要多出一列,按地区 排名 :

北京地区 bb 10 1
北京地区 aa 20 2
天津地区 aa 30 1
天津地区 bb 40 2


大虾,怎么写sql...............
...全文
177 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangxiao_jiang 2010-10-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 redspr 的回复:]

select c1,c2,c3,row_number() over(partition by c1 order by c3) as c4 from tt order by c1,c3
[/Quote]
这个是标准的db2的函数用法。
Mr_Bean 2010-10-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 redspr 的回复:]
select c1,c2,c3,row_number() over(partition by c1 order by c3) as c4 from tt order by c1,c3
[/Quote]

我推荐这个 ~~~~

也可参考rank over
wwwwb 2010-10-21
  • 打赏
  • 举报
回复
楼上不错,要 7。X以上版本支持
redspr 2010-10-21
  • 打赏
  • 举报
回复
select c1,c2,c3,row_number() over(partition by c1 order by c3) as c4 from tt order by c1,c3
zhaojianmi1 2010-10-21
  • 打赏
  • 举报
回复
select a.col1,a.col2,a.col3,count(b.col3) from tt a left join tt b on a.col1=b.col1 and a.col3>=b.col3
group by a.col1,a.col2,a.col3 order by a.col1,count(b.col3)
ACMAIN_CHM 2010-10-21
  • 打赏
  • 举报
回复
select *,(select count(*) from 数据 where 地区=a.地区 and col3<=a.col3) as 排名
from 数据 a
order by 北京地区,col3
wwwwb 2010-10-21
  • 打赏
  • 举报
回复
f1 f2 f3
北京地区 aa 20
天津地区 aa 30
北京地区 bb 10
天津地区 bb 40

select a.f1,a.f2,a.f3,count(b.f3) from tt a left join tt b on a.f1=b.f1 and a.f3>=b.f3
group by a.f1,a.f2,a.f3 order by a.f1,count(b.f3)
ww521bmw 2010-10-21
  • 打赏
  • 举报
回复
谢谢大家。。。

5,889

社区成员

发帖
与我相关
我的任务
社区描述
IBM DB2 是美国IBM公司开发的一套关系型数据库管理系统,它主要的运行环境为UNIX(包括IBM自家的AIX)、Linux、IBM i(旧称OS/400)、z/OS,以及Windows服务器版本
社区管理员
  • DB2
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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