Answer the question
In order to leave comments, you need to log in
Transposing a SQL table
faced with the task of transposing a table (rotation by 90 degrees).
have columns
A | b | C
1 | 2 | 3
4 | 5 | 6
output
A 1 4
B 2 5
C 3 6
on the Internet they write
"my very strong advice: don't try to do this with SQL"
and a couple of rather "strange" options
What do you advise?
Answer the question
In order to leave comments, you need to log in
It's not entirely clear what type of data it is, but it's fashionable enough to solve using Dynamic SQL anyway.
A | b | C
1 | 2 | 3
4 | 5 | 6
rotations 90 degrees left:
C 3 6
B 2 5
A 1 4
right:
4 1 A
5 2 B
6 3 C
Transpose - Replace columns with rows, not rotate!
If there is not too much data and the task is one-time, the procedure is very simple:
1. Count everything.
2. Transpose.
3. Record everything.
I wonder where such a task comes from.
By the way, DBMS, in fact, are able, in a sense, to transpose tables. This is called an "index".
It is unlikely that this will help you, but in MS-SQL this is done using the PIVOT function. Maybe google on "pivot in MySql" will help
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question