Skip to content
Advertisement

How to use data from multiple similar columns as rows in SQL

I have a table as below:

ItemName 1mth_presale_cost 2mth_presale_cost 1mth_postsale_cost 2mth_postsale_cost
1000 10.1 12.1 12.5 15.1
1001 20.2 15.2 25.2 17.3

I want the result to be like below table:

ItemName 1mth_cost 2mth_cost
1000 10.1 12.1
1000 12.5 15.1
1001 20.2 15.2
1001 25.2 17.3

I don’t want to use UNION for this.

Advertisement

Answer

First each row converted into two sets then apply conditional CASE statement for getting desired result.

-- SQL Server
SELECT t.ItemName
     , CASE WHEN p.id = 1 THEN [1mth_presale_cost] ELSE [1mth_postsale_cost] END  "1mth_cost"
     , CASE WHEN p.id = 1 THEN [2mth_presale_cost] ELSE [2mth_postsale_cost] END  "2mth_cost"
FROM test t
CROSS JOIN (SELECT 1 id UNION SELECT 2 id) p
ORDER BY t.ItemName, p.id

Please check from url https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=3e1fdaf829c39554941c068b0c8a3e04

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