问请,如何查询能得到指定字段相同的上一条数据?

ayurep 2007-12-06 04:29:01
问请,如何查询能得到指定字段相同的上一条数据?

具体是这样的:
有一表Mart_Price,有name,price,mill,UpDateTime,...等字段

我须要查询出如:按UpDateTime排序,第一条记录的name和mill和表中下一条记录相同的结果,应该如何写这个sql呢?

不知道我有没说清楚,主要作用是今天的数据和昨天得数据做比较用的.
...全文
80 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
elvis_gao 2007-12-06
  • 打赏
  • 举报
回复


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

*/

ayurep 2007-12-06
  • 打赏
  • 举报
回复
select a.*,(select top 1 cc_priceID from LX_Mart_Price where cc_priceID<a.cc_priceID and cc_name = a.cc_name and cc_spec = a.cc_spec and cc_mill = a.cc_mill order by updatetime desc) as nextNum
from LX_Mart_Price a

谢谢大家,已经搞定,


不知道有没有更优的写法,哈哈.




ayurep 2007-12-06
  • 打赏
  • 举报
回复
charry0110 是你的那种思路,能帮看看具体对应该到上面那个表,应该怎么写这个sql呢.

我还菜...
火星求索 2007-12-06
  • 打赏
  • 举报
回复

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
ayurep 2007-12-06
  • 打赏
  • 举报
回复
http://cn.cclxw.com/Price/webform1.aspx

比如上面这个表中,这里的cc_PriceID为4846的第一条记录中cc_name cc_MPa cc_spec和三他字段完全相同的应该是次日的
4668这条记录,

我要得到的结果是,再再这个表上加个字段为cc_zd(涨跌),能计算出cc_price字段4846和4668之间的差值.
火星求索 2007-12-06
  • 打赏
  • 举报
回复

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
ayurep 2007-12-06
  • 打赏
  • 举报
回复
这里不能贴图得嘛,不方便.

happyflystone 3Q,我试试

不行,我找个地方上图给大家看看,谢啦
fa_ge 2007-12-06
  • 打赏
  • 举报
回复
楼主要拿点数据出来,这样才能准确得到答案
-狙击手- 2007-12-06
  • 打赏
  • 举报
回复
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
tomyuansir 2007-12-06
  • 打赏
  • 举报
回复
没看懂题目!!

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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