数据库表的加减乘除
发布网友
发布时间:2022-04-29 17:23
我来回答
共1个回答
热心网友
时间:2022-06-25 13:33
如果该表的year+number不唯一,则需要提供一个具有唯一值的字段或字段组.
现假设year,number唯一,sql语句写法如下:
select
a.[id] as IDA,b.[id] as IDB,a.[year] as YEARA,b.[year] as YEARB,
a.[number] as NumberA,b.[number] as NumberB,a.[text] as testA,b.[text] as testB
from [tab1] a ,[tab1] b
where a.[year]=(select top 1 x.[year] from [tab1] x where x.[number]=b.[number] and x.[year]>=b.[year]-1 order by x.[year]) and a.[number]=b.[number]
and a.[year]!=b.[year]
order by a.[id]