1

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

所以如果桌子像

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

这可能吗?

4

2 回答 2

3

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

SELECT field1 + lead(field1) over (order by field1) as "Concatenated Field"
from table
于 2012-04-05T16:13:18.100 回答
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
于 2012-04-05T16:08:56.727 回答