0

I have a table like this:

id P C A B
1 100 3 a1 b1
2 101 3 a2 b2
3 102 3 a3 b3
4 103 3 a4 b4
5 100 4 a5 b5
6 101 4 a6 b6
7 102 4 a7 b7
8 103 4 a8 b8

I want to get a new transposed structure like this:

P _3A _3B _4A _4B
100 a1 b1 a5 b5
101 a2 b2 a6 b6
102 a3 b3 a7 b7
103 a4 b4 a8 b8

As you can see ,new field names have been extracted from C field in the original table. Is there any way to do this using SQL?

3
  • What is your database? Commented Nov 9, 2014 at 11:32
  • My database is PostgreSQL. Commented Nov 9, 2014 at 11:35
  • Search for sql pivot. Commented Nov 9, 2014 at 11:40

2 Answers 2

2

Postgres has some advanced functionality in terms of arrays and crosstab. However, a database independent way of doing this is by using aggregation:

select t.p,
       max(case when c = 3 then a end) as a3,
       max(case when c = 3 then b end) as b3,
       max(case when c = 4 then a end) as a4,
       max(case when c = 4 then b end) as b4
from atable t
group by t.p;

This will work in both SQLite and Postgres (and just about any other database).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Gordon. It Works.
2

Test Data

DECLARE @TABLE TABLE (id INT, P INT, C INT, A VARCHAR(2), B VARCHAR(2))
INSERT INTO @TABLE VALUES 
(1  ,100 ,3  ,'a1','b1'),
(2  ,101 ,3  ,'a2','b2'),
(3  ,102 ,3  ,'a3','b3'),
(4  ,103 ,3  ,'a4','b4'),
(5  ,100 ,4  ,'a5','b5'),
(6  ,101 ,4  ,'a6','b6'),
(7  ,102 ,4  ,'a7','b7'),
(8  ,103 ,4  ,'a8','b8')

Query

SELECT * FROM 
 (
    SELECT P , Vals , '_' + CAST(C AS VARCHAR(10)) + N  AS Cols
    FROM @TABLE 
        UNPIVOT (Vals FOR N IN (A, B))up
 )A
 PIVOT (MAX(Vals)
        FOR Cols 
        IN ([_3A],[_3B],[_4A],[_4B])
        )p

Result

╔═════╦═════╦═════╦═════╦═════╗
║  P  ║ _3A ║ _3B ║ _4A ║ _4B ║
╠═════╬═════╬═════╬═════╬═════╣
║ 100 ║ a1  ║ b1  ║ a5  ║ b5  ║
║ 101 ║ a2  ║ b2  ║ a6  ║ b6  ║
║ 102 ║ a3  ║ b3  ║ a7  ║ b7  ║
║ 103 ║ a4  ║ b4  ║ a8  ║ b8  ║
╚═════╩═════╩═════╩═════╩═════╝

2 Comments

Wow! Thank you M.Ali. And does it work with sqlite also? Because I use SQLite in development environment. But my final dbms will be PostgreSQL.
First when you put up your question you didnt mention what RDBMS you were using, I assumed it was SQL Server and to know knowledge this should work 100% with SQL Server not to sure about other RDBMS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.