导航
  • 主页
  • 基础类
  • 应用实例
  • 新技术前沿

求一简单经典语句。

Haten 2008-01-16 08:32:18
TABLE A:
F_1 F_2
AA 1
BB 3
CC 7

如何得到以下结果:
F_1 F_2
AA 1
BB 2
CC 4

即:下一行减去上一行F_2的值。
...全文
945 点赞 收藏 11
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Haten 2008-01-17
多谢大家!
回复
Haten 2008-01-17
还有这种情况:
TABLE A:
F_1 F_2 SUM
0 0 100
30 0 0
0 10 0

得到结果:
F_1 F_2 SUM
0 0 100
30 0 130
0 10 120

既:下一行的SUM等于上一行的SUM加上下一行的(F_1-F2)
回复
JL99000 2008-01-17
if object_id('tb') is not null
drop table tb
go
create table tb (F_1 varchar(10),F_2 int)
insert into tb
select 'AA',1 union all
select 'BB',3 union all
select 'CC',7

declare @t table(id int identity(1,1),F_1 varchar(10),F_2 int)
insert into @t select * from tb
select top 1 F_1,F_2 from @t
union all
select a.F_1,b.F_2-a.F_2 as F_2 from @t a,@t b
where a.id=b.id-1

接分喽
回复
谁是谁的谁 2008-01-16
select *, px=row_number() over(order by f1)from @tb )a
这一句怎么理解呢?
回复
中国风 2008-01-16
declare @T  table (F_1 varchar(10) , F_2 int)
insert into @T values('AA', 1 )
insert into @T values('BB', 3 )
insert into @T values('CC', 7 )

--根据F_1列判断
select
F_1,F_2-isnull((select F_2 from @T where F_1=(select max(F_1) from @T where F_1<t.F_1)),0) as F_2
from
@T t
/*

(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)

F_1 F_2
---------- -----------
AA 1
BB 2
CC 4

(所影响的行数为 3 行)


*/

---2005
select
t.F_1,t.F_2-t2.F_2 as F_2
from
(select *, row=row_number()over(order by F_1)from @T)T
left join
(select *, row=row_number()over(order by F_1)from @T)T2 on t.row=T2.row-1


-----------2000没有F_1\F_2没有大小关系时,只有通过生成临时表实现
回复
dobear_0922 2008-01-16
--随手敲的,不排除手误
create table tb(F_1 varchar(8) , F_2 int)
insert into tb values('AA', 1 )
insert into tb values('BB', 3 )
insert into tb values('CC', 7 )
go

select a.F_1, F_2=a.F_2-isnull(max(b.F_2),0)
from tb a left join tb b on a.F_2>b.F_2
group by a.F_1, a.F_2

drop table tb
回复
dawugui 2008-01-16
[code=SQL]create table tb(F_1 varchar(10) , F_2 int)
insert into tb values('AA', 1 )
insert into tb values('BB', 3 )
insert into tb values('CC', 7 )
go

select tb.* from tb where f_1 in (select min(f_1) from tb)
union all
select t2.f_1 , f_2 = t2.f_2 - t1.f_2 from
(SELECT * , px = (SELECT COUNT(f_1) FROM tb WHERE f_1 < t.f_1) + 1 FROM tb t) t1,
(SELECT * , px = (SELECT COUNT(f_1) FROM tb WHERE f_1 < t.f_1) + 1 FROM tb t) t2
where t1.px = t2.px - 1

drop table tb

/*
F_1 F_2
---------- -----------
AA 1
BB 2
CC 4

(所影响的行数为 3 行)
*/
[/code]
回复
dawugui 2008-01-16
create table tb(F_1 varchar(10) , F_2 int)
insert into tb values('AA', 1 )
insert into tb values('BB', 3 )
insert into tb values('CC', 7 )
go

select tb.* from tb where f_1 in (select min(f_1) from tb)
union all
select t2.f_1 , f_2 = t2.f_2 - t1.f_2 from
(SELECT * , px = (SELECT COUNT(f_1) FROM tb WHERE f_1 < t.f_1) + 1 FROM tb t) t1,
(SELECT * , px = (SELECT COUNT(f_1) FROM tb WHERE f_1 < t.f_1) + 1 FROM tb t) t2
where t1.px = t2.px - 1

drop table tb

/*
F_1 F_2
---------- -----------
AA 1
BB 2
CC 4

(所影响的行数为 3 行)
*/
回复
pt1314917 2008-01-16
declare @a table(F_1 varchar(10),F_2 int) 
insert into @a select 'AA', 1
insert into @a select 'BB', 3
insert into @a select 'CC', 7


select id=identity(int,1,1),* into #temp from @a


select a.f_1,isnull(a.f_2-b.f_2,a.f_2)f_2 from #temp a left join #temp b
on a.id=b.id+1
回复
wzy_love_sly 2008-01-16
declare @tb table (f1 varchar(10),f2 int)
insert into @tb select 'aa',1
insert into @tb select 'bb',3
insert into @tb select 'cc',7
select a.f1,a.f2-isnull(b.f2,0) as '差' from (
select *, px=row_number() over(order by f1)from @tb )a
left join(
select *, px=row_number() over(order by f1)from @tb )b
on a.px=b.px+1


f1 差
aa 1
bb 2
cc 4
回复
dawugui 2008-01-16
select f_1 , f_2 = (select min(f_2) from tb where f_2 > t.f_2) - f_2 from tb t
回复
发动态
发帖子
MS-SQL Server
创建于2007-09-28

3.2w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
社区公告
暂无公告