选择下一行

时间:2012-04-05 15:35:21

标签: sql sql-server tsql sql-server-2008-express

我想将当前行中的两个文本字段与下一行中的相同字段连接起来

所以如果表格就像

field1  field2  field3

text1    text3  order1
text2    text4  order1

我想这样做:

if (field3.current_row = field3.next_row)
     SELECT field1 + getNextRow(field1) as "Concatenated Field" FROM table

这可能吗?

2 个答案:

答案 0 :(得分:3)

如果您已经使用SQL Server 2012 ,则可以执行以下操作:

SELECT field1 + lead(field1) over (order by field1) as "Concatenated Field"
from table

答案 1 :(得分:2)

你可以做类似的事情:

create table #temp
(
    field1 varchar(50),
    field2 varchar(50)
)

insert into #temp values ('text1', 'text3')
insert into #temp values ('text2', 'text4')

;with cte as
(
    select *, row_number()over(order by field1) as rownum
    from #temp
)
SELECT *
FROM 
(
    select c1.field1 + ' ' + (SELECT field1 FROM cte c2 WHERE c2.rownum = c1.rownum + 1) as ConcField
    from cte c1
) c
where c.concfield is not null

drop table #temp