create table #tmp1
( id int,
datetime datetime,
code     int, 
name varchar(200),
bid    float
)
create index idx_#tmp1 on #tmp1(datetime,code)insert into #tmp1
 select 142425     , '2006-4-28 10:53:00',     1,     '铜',    67300
union select 141819     , '2006-4-27 10:58:00',     2,     '锡',    85000
union select 141817     , '2006-4-27 10:58:00',     1,     '铜',    69550
union select 141191     , '2006-4-26 10:57:00',     2,     '锡',    84500
union select 141177     , '2006-4-26 10:52:00',     1,     '铜',    68000 --select * from #tmp1
select a.id,a.datetime,a.code,a.name,isnull(a.bid - b.bid,0)
from #tmp1 a left join #tmp1 b on  convert(varchar,a.datetime,112) = dateadd(day,1,convert(varchar,b.datetime,112)) and a.code = b.code 

解决方案 »

  1.   

    id               datetime         code name    bid
    142425      2006-4-28 10:53:00     1     铜    67300
    141819      2006-4-27 10:58:00     2     锡    85000
    141817      2006-4-27 10:58:00     1     铜    69550
    141191      2006-4-26 10:57:00     2     锡    84500
    141177      2006-4-26 10:52:00     1     铜    68000分组求铜,锡4月最初价格,最后价格,平均价格这个怎么搞谢谢了