sybase取行号,或者多行合并为一行

arcanine_asdn 2014-08-29 05:48:04
现在有个问题,需要给查询结果加行号,而且行号要分组,不同ID的行,要启用新的行号。
或者可以将多行数据合并为一行也可以。
比如数据是这样
ID num
1 0
1 1
1 1
2 1
2 2
2 0
我想要的显示结果是
ID num rownum
1 0 1
1 1 2
1 1 3
2 1 1
2 2 2
2 0 3
或者
ID str
1 011
2 120
不知道大家有没有什么方法。
我的数据库是sybaseASE15.0.3

...全文
1475 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
火星上的海 2015-01-06
  • 打赏
  • 举报
回复
declare @gid int declare @str varchar(500) declare @num int set @gid=0 set @num=1 select id,num,space(500) as str,0000 as rownumber into #tt from tablename update #tt set str=(case when @gid=id then @str + ',' + convert(varchar,num) else convert(varchar,num) end) , @str=(case when @gid=id then @str + ',' + convert(varchar,num) else convert(varchar,num) end), rownumber=(case when @gid=id then @num+1 else 1 end), @num=(case when @gid=id then @num+1 else 1 end), @gid=id ---分组加行号 select id,num,rownumber from #tt ---将多行数据合并为一行 select a.id,a.str from #tt a inner join (select id,max(rownumber) as ma from #tt group by id) b on a.id=b.id and a.rownumber=ma
dellusion 2014-12-16
  • 打赏
  • 举报
回复
我遇到过得 而且解决了 类似并列第一的问题 用的是dw的分组功能

2,596

社区成员

发帖
与我相关
我的任务
社区描述
Sybase相关技术讨论区
社区管理员
  • Sybase社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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