34,837
社区成员




drop table #t2
go
create table #t2(cc_PriceID int, cc_ClassID int, cc_name nvarchar(20), cc_MPa nvarchar(20), cc_price int, UpDateTime datetime)
insert into #t2 select 4846, 2, '螺纹钢', 'HRB335' , 4120 , '2007-8-29 15:33:00'
insert into #t2 select 4845, 2, '螺纹钢' ,'HRB335' ,3820 ,'2007-8-29 15:32:00'
insert into #t2 select 4742, 2, '二级螺纹钢', 'HRB335' ,4010 , '2007-8-24 11:34:00'
insert into #t2 select 4741, 2, '二级螺纹钢' ,'HRB335', 3880 , '2007-8-24 11:33:00'
insert into #t2 select 4669, 2, '螺纹钢', 'HRB335' ,3780 , '2007-8-20 17:32:00'
insert into #t2 select 4668, 2, '螺纹钢' ,'HRB335' , 4080 , '2007-8-20 17:31:00'
insert into #t2 select 4589, 2, '螺纹钢', 'HRB335' , 4000, '2007-8-13 13:09:00'
insert into #t2 select 4588, 2, '螺纹钢', 'HRB335', 3700, '2007-8-13 13:08:00'
insert into #t2 select 4542, 2, '螺纹钢', 'HRB335',3700, '2007-8-10 13:59:00'
insert into #t2 select 4543, 2, '螺纹钢', 'HRB335' , 3970, '2007-8-10 13:59:00'
go
alter table #t2 add cc_zd int
go
update #t2
set cc_zd =
cc_price-(select a.cc_price
from #t2 a where
a.cc_ClassID=#t2.cc_ClassID and a.cc_name=#t2.cc_name and a.cc_MPa=#t2.cc_MPa
and datediff(dd,a.UpDateTime,#t2.UpDateTime)=0 and #t2.cc_PriceID>a.cc_PriceID)
go
select * from #t2
/*
cc_PriceID cc_ClassID cc_name cc_MPa cc_price UpDateTime cc_zd
----------- ----------- -------------------- -------------------- ----------- ----------------------- -----------
4846 2 螺纹钢 HRB335 4120 2007-08-29 15:33:00.000 300
4845 2 螺纹钢 HRB335 3820 2007-08-29 15:32:00.000 NULL
4742 2 二级螺纹钢 HRB335 4010 2007-08-24 11:34:00.000 130
4741 2 二级螺纹钢 HRB335 3880 2007-08-24 11:33:00.000 NULL
4669 2 螺纹钢 HRB335 3780 2007-08-20 17:32:00.000 -300
4668 2 螺纹钢 HRB335 4080 2007-08-20 17:31:00.000 NULL
4589 2 螺纹钢 HRB335 4000 2007-08-13 13:09:00.000 300
4588 2 螺纹钢 HRB335 3700 2007-08-13 13:08:00.000 NULL
4542 2 螺纹钢 HRB335 3700 2007-08-10 13:59:00.000 NULL
4543 2 螺纹钢 HRB335 3970 2007-08-10 13:59:00.000 270
*/
declare @t table(ID int ,Num int)
insert into @t select 1,1
insert into @t select 2,1
insert into @t select 3,2
insert into @t select 4,2
insert into @t select 5,3
select a.*,(select top 1 Num from @t where ID>a.ID and Num=a.Num) as nextNum from @t a
ID Num nextNum
----------- ----------- -----------
1 1 1
2 1 NULL
3 2 2
4 2 NULL
5 3 NULL
declare @t table(ID int ,Num int)
insert into @t select 1,1
insert into @t select 2,1
insert into @t select 3,2
insert into @t select 4,2
insert into @t select 5,3
select a.*,(select top 1 Num from @t where ID>a.ID) as nextNum from @t a
select *
from tablea a
left join tablea b
on a.name = b.name and a.mill = b.mill and a.updatetime > b.updatetime
where a.id = ?
order by updatedatime desc