是不是日期没有重复的?select a.*,
       数值-isnull( ( select top 1 数值 
                      from tablename
                      where 日期<a.日期 
                      order by 日期 desc
                    ),0
                  ) as 差值
from tablename a

解决方案 »

  1.   

    select a.日期,a.數值,差值=a.數值-isnull(b.數值,0)      
    from 表 a,(select 數值 from 表 where 日期=dateadd(day,-1,日期)) b
    where a.日期=b.日期
      

  2.   

    呵呵,来晚了, victorycyz的更通用, 
     jackting认为日期是相差一天的。
    就不知楼主到底要什么样的。
      

  3.   

    --用自连接的写法:select a.自动编号,a.日期,a.数值
    ,差值=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)
      

  4.   

    --上面写错了,改一下: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
      

  5.   

    --测试--测试数据
    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 行)
    --*/
      

  6.   

    --如果日期并不按顺序,则改为: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.日期
      

  7.   

    --测试--测试数据
    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 行)--*/