急,在线等小问题

dada9527 2004-12-02 07:30:40
select怎么后边的减前边的呀?

有一列
col
1
3
6
9
15
22


怎么整??
3-1
6-3
9-6
15-9
22-15
...全文
197 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ncaidexiaoniao 2004-12-02
  • 打赏
  • 举报
回复
create table test(col numeric(10))
Insert into test
select '1'
union all select '3'
union all select '6'
union all select '9'
union all select '15'
union all select '22'
select id=identity(int),col into #t from test
drop table #t
select * from #t
select b.col-a.col col from (select * from #t)a,(select * from #t where id>1)b
where b.id=a.id+1
drop table #t
还是用临时表的好
dada9527 2004-12-02
  • 打赏
  • 举报
回复
还有别的办法没?
Andy__Huang 2004-12-02
  • 打赏
  • 举报
回复
不用臨時表我不知道了?象這種最好是用臨時表了
dada9527 2004-12-02
  • 打赏
  • 举报
回复
我天,振么麻烦腻呀
clx333 2004-12-02
  • 打赏
  • 举报
回复
楼上的真快啊
dada9527 2004-12-02
  • 打赏
  • 举报
回复
先谢谢海洋
Andy__Huang 2004-12-02
  • 打赏
  • 举报
回复
create table tb(col numeric(10))
Insert into tb
select '1'
union all select '3'
union all select '6'
union all select '9'
union all select '15'
union all select '22'

select id=identity(int,1,1),col into tab from tb

select col=a.col-b.col from (select * from tab where id>1)a
left join tab b on a.id=b.id+1

drop table tab

--結果
col
----------
2
3
3
6
7
dada9527 2004-12-02
  • 打赏
  • 举报
回复
噢,不用林时表不成吗?
Andy__Huang 2004-12-02
  • 打赏
  • 举报
回复
改一下

select id=identity(int,1,1),col into tab from tb

select col=a.col-b.col from (select * from tab where id>1)a
left join tab b on a.id=b.id+1

drop table tab
Andy__Huang 2004-12-02
  • 打赏
  • 举报
回复
這個要用臨時表

select id=identity(int,1,1),col into tab from tb

select col=a.col-b.col from (select * from tab where id>1)a
left join tab b on a.id=b.id

drop table tab


dada9527 2004-12-02
  • 打赏
  • 举报
回复
人呢人呢

34,590

社区成员

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

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