select x.字段1,y.字段1 from ( select *,ROW_NUMBER() over(order by 字段1) as rowid from A ) as X ,( select *,ROW_NUMBER() over(order by 字段1) as rowid from A ) as Y where x.rowid=y.rowid+1 and x.字段2='+' and Y.字段2='-'
1楼的 + 应该改成 - 号! create table csdn_sql(name1 varchar(20),name2 varchar(20)) insert into csdn_sql select 'a','+' union select 'b','+' union select 'c', '-' union select 'd', '+' union select 'e' ,'-' union select 'f' ,'+' union select 'g', '+' union select 'h', '+' union select 'i', '-' union select 'j', '+' union select 'k', '+'select * from csdn_sqlselect ROW_NUMBER() over(order by x.name1) as id,x.name1,y.name1 from (select *,ROW_NUMBER() over(order by name1) as rowid from csdn_sql ) as X , (select *,ROW_NUMBER() over(order by name1) as rowid from csdn_sql) as Y where x.rowid =y.rowid -1 and x.name2='+' and y.name2='-' 经测试,没有问题!直接贴代码,就可以看效果!
from
(
select *,ROW_NUMBER() over(order by 字段1) as rowid
from A
) as X ,(
select *,ROW_NUMBER() over(order by 字段1) as rowid
from A
) as Y
where x.rowid=y.rowid+1 and x.字段2='+' and Y.字段2='-'
create table csdn_sql(name1 varchar(20),name2 varchar(20))
insert into csdn_sql select 'a','+'
union select
'b','+'
union select
'c', '-'
union select
'd', '+'
union select
'e' ,'-'
union select
'f' ,'+'
union select
'g', '+'
union select
'h', '+'
union select
'i', '-'
union select
'j', '+'
union select
'k', '+'select * from csdn_sqlselect ROW_NUMBER() over(order by x.name1) as id,x.name1,y.name1 from
(select *,ROW_NUMBER() over(order by name1) as rowid from csdn_sql ) as X
,
(select *,ROW_NUMBER() over(order by name1) as rowid from csdn_sql) as Y where x.rowid =y.rowid -1 and x.name2='+' and y.name2='-'
经测试,没有问题!直接贴代码,就可以看效果!