Vivek
Vivek

Reputation: 329

How to pivot this

BA21 YEAR   109 10  119 EBC
BA21 YEAR   9   0   9   FP
BA21 YEAR   3   0   3   BC

My data is like this and I need to convert this to

BA21 YEAR 109 10 119 EBC 9 0 9 FP 3 0 3 BC

How to do this?

Upvotes: 0

Views: 131

Answers (1)

Adriaan Stander
Adriaan Stander

Reputation: 166406

THis does not seem like PIVOTing, it more seems like a self join.

Something like

SELECT t1.*,t2.column1,t2.column2,...,t3.column1,t3.column2,...
FROM Table1 t1 INNER JOIN
Table2 t2 ON t1.PrimaryKey = t2.PrimaryKey INNER JOIN
Table3 t3 ON t1.PrimaryKey = t3.PrimaryKey

where you are selecting only the columns from Table2 and Table3 that you need.

Also, this can be changed to LEFT joins if required. Have a look at Introduction to JOINs – Basic of JOINs for JOIN explenations.

Upvotes: 1

Related Questions