是不是日期没有重复的?select a.*,
数值-isnull( ( select top 1 数值
from tablename
where 日期<a.日期
order by 日期 desc
),0
) as 差值
from tablename a
解决方案 »
- 一个简单的SQL语句问题
- 请教一下如何将 getdate()得到的datetime 型据转换.
- SQL MAIL
- sql语句的where的范围
- SQL Server2000中复制(Replication)起什么作用,在什么情况下我们可以用到!
- sql2000 7000多万条记录,分页查询谁有什么好方法?
- 求救:年龄低于作者平均年龄的所有作者的作者名、书名和出版社
- sql2000 存储函数如何返回一个int数组?大家给个实例吧,谢谢了 !
- 关于缺省值的问题?
- sql server7.0 的 full text index 是干什么用的? 功能如何?
- 存储过程传入参数类型
- 临时表建立后是否可建立索引?
from 表 a,(select 數值 from 表 where 日期=dateadd(day,-1,日期)) b
where a.日期=b.日期
jackting认为日期是相差一天的。
就不知楼主到底要什么样的。
,差值=isnull(a.数值,0)-isnull(b.数值,0)
from 表 a join 表 b
on (
select count(*) from 表 where id<=a.id
)+1=(
select count(*) from 表 where id<=b.id)
,差值=isnull(a.数值,0)-isnull(b.数值,0)
from 表 a left join 表 b
on (select count(*) from 表 where 自动编号<=a.自动编号)
=(select count(*) from 表 where 自动编号<=b.自动编号)+1
create table 表(
自动编号 int primary key, --为了方便说明编号不连续的问题,编号自己输入
日期 datetime,
数值 int)
insert 表 select 1,'2003-01-01',10
union all select 2,'2003-02-01',11
union all select 4,'2003-03-01',22
union all select 5,'2003-04-01',44
union all select 9,'2003-05-01',67
go--查询
select a.自动编号,a.日期,a.数值
,差值=isnull(a.数值,0)-isnull(b.数值,0)
from 表 a left join 表 b
on (select count(*) from 表 where 自动编号<=a.自动编号)
=(select count(*) from 表 where 自动编号<=b.自动编号)+1
go--删除测试
drop table 表/*--测试结果自动编号 日期 数值 差值
----------- -------------------------- ----------- ---------
1 2003-01-01 00:00:00.000 10 10
2 2003-02-01 00:00:00.000 11 1
4 2003-03-01 00:00:00.000 22 11
5 2003-04-01 00:00:00.000 44 22
9 2003-05-01 00:00:00.000 67 23(所影响的行数为 5 行)
--*/
,差值=isnull(a.数值,0)-isnull(b.数值,0)
from 表 a left join 表 b
on (
select count(*) from 表
where 日期<a.日期
or(日期=a.日期 and 自动编号<=a.自动编号)
)=(
select count(*) from 表
where 日期<b.日期
or(日期=b.日期 and 自动编号<=b.自动编号)
)+1
order by a.日期
create table 表(
自动编号 int primary key, --为了方便说明编号不连续的问题,编号自己输入
日期 datetime,
数值 int)
insert 表 select 1,'2003-01-01',10
union all select 2,'2003-03-01',11
union all select 4,'2003-02-01',22
union all select 5,'2003-04-01',44
union all select 9,'2003-03-01',67
go--查询
select a.自动编号,a.日期,a.数值
,差值=isnull(a.数值,0)-isnull(b.数值,0)
from 表 a left join 表 b
on (
select count(*) from 表
where 日期<a.日期
or(日期=a.日期 and 自动编号<=a.自动编号)
)=(
select count(*) from 表
where 日期<b.日期
or(日期=b.日期 and 自动编号<=b.自动编号)
)+1
order by a.日期
go--删除测试
drop table 表/*--测试结果自动编号 日期 数值 差值
----------- --------------------------- ----------- -------
1 2003-01-01 00:00:00.000 10 10
4 2003-02-01 00:00:00.000 22 12
2 2003-03-01 00:00:00.000 11 -11
9 2003-03-01 00:00:00.000 67 56
5 2003-04-01 00:00:00.000 44 -23(所影响的行数为 5 行)--*/