具有挑战性的sql语句,高手请进!!

ljian_mail 2004-07-12 04:17:03
表a
序号 异动前数量 异动数量 异动后数量
2 10 10 20
5 20 -10 10
100 11 1 12
120 12 5 17
121 17 2 19
123 23 -10 13
130 13 1 14
153 14 2 16
.
.
用一个sql语句找出异动前数量不等于上一条记录异动后数量。
该怎么写??
...全文
99 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zheninchangjiang 2004-07-12
  • 打赏
  • 举报
回复
抢楼是个乐趣,学到是成绩
dqjia 2004-07-12
  • 打赏
  • 举报
回复
晕死!!!
看到题目—>写语句—>建表验证—>发出回复—>已经结贴
没法混了....
popmailzjw 2004-07-12
  • 打赏
  • 举报
回复
create table dbo.ss(a int,b int,c int,d int)
go
insert into ss
select 2 , 10 , 10 , 20 union all
select 5 , 20 , -10 , 10 union all
select 100 , 11 , 1 , 12 union all
select 120 , 12 , 5 , 17 union all
select 121 , 17 , 2 , 19 union all
select 123 , 23 , -10 , 13 union all
select 130 , 13 , 1 , 14 union all
select 153 , 14 , 2 ,16
go
select IDENTITY(smallint, 100, 1) as e, * into dbo.s from ss
go
select a,b,c,d from(
select s.*,s0.b as d1
from s inner join s as s0 on s.e+1=s0.e-- and s.d<>s0.d
) as t where d<>d1
go
drop table dbo.s
drop table dbo.ss
go

結果=================================
5 20 -10 10
121 17 2 19

=====================================
不知道合不合要求﹐用一個語句我不知道怎么做﹐用這種死方法就會﹐呵呵

算是丟個石頭吧﹐樓下的有玉的就扔出來吧
dqjia 2004-07-12
  • 打赏
  • 举报
回复
select 序号,异动前数量,异动后数量,isnull((select top 1 异动后数量 from a where 序号<c.序号 order by 序号 desc),0) 前行数量 from a as c where isnull((select top 1 异动后数量 from a where 序号<c.序号 order by 序号 desc),0)<>异动前数量


这里假设序号是升序的
zheninchangjiang 2004-07-12
  • 打赏
  • 举报
回复
select * from a aa where 异动后数量<>(select 异动前数量 from a where 序号=(select min(序号) from a where 序号>aa.序号))
victorycyz 2004-07-12
  • 打赏
  • 举报
回复

select *
from a b
where 异动前数量<>( select top 1 异动后数量
from a
where 序号<b.序号
order by 序号 desc
)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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