Pran
Pran

Reputation: 155

SQL : Query to get single row data in two rows

I have a table in below format

enter image description here

and I want output like this

enter image description here

that means i want to show

Column 1,Column 2,Column 3,Column 4,Column 5,Column 6,Column 7

in First row and

Column 1,Column 2,Column 3,Column 4,Column 8,Column 9,Column 10

in Second row

and main problem is i am having 256 columns and i dont want to run same query again and again. So is there any way to solve this using loop or anything.?

Thank you in advance

Upvotes: 1

Views: 390

Answers (1)

DB101
DB101

Reputation: 633

You can get this by using a union query. Add the OrderKey column to maintain the ordering of your rows.

SELECT Column1,Column2,Column3,Column4,Column5,Column6,Column7, 1 AS OrderKey
FROM [TableName]
UNION ALL
SELECT Column1,Column2,Column3,Column4,Column8,Column9,Column10, 2 AS OrderKey
FROM [TableName]
ORDER BY OrderKey

Upvotes: 2

Related Questions