Skip to content
Advertisement

Concatenate values of two consecutive rows SQL

I want to write a SQL query to concatenate row values of 2 consecutive rows as described below:

I have the below table:

I want to have another column as below:

The concat value is the concatenation of serial_no value in row 2 with the serial_no value is row 1, serial_no value in row 3 with the serial_no value is row 2 and so on. Since row 1 has no rows above it, the value remains as it is.

How can I achieve this?

Advertisement

Answer

Assuming that SerialNo defines your order, then you can use LAG() to get the value from the previous row:

If Serial_No doesn’t define your order, then you can amend the order by as required.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement