3
3
3ybHa9_Fe92010-09-22 18:03:38
MySQL
3ybHa9_Fe9, 2010-09-22 18:03:38

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

7 answer(s)
B
biduin, 2010-09-22
@3ybHa9_Fe9

It's not entirely clear what type of data it is, but it's fashionable enough to solve using Dynamic SQL anyway.

D
DanXai, 2010-09-23
@DanXai

A | b | C
1 | 2 | 3
4 | 5 | 6
output
A 1 4
B 2 5
C 3 6

D
DanXai, 2010-09-23
@DanXai

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!

H
Horse, 2010-09-22
@Horse

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.

K
kmike, 2010-09-23
@kmike

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".

N
nZeus, 2010-09-23
@nZeus

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

S
Stanislav Agarkov, 2010-09-24
@stas_agarkov

when already a blunt muscle is to learn how to do it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question