如何将列值转换为行

时间:2021-09-12 13:12:54

I will try to explain my question.

我会尽量解释我的问题。

My table structure:

我的表结构:

name Code_1 Code_1_value Code_2 Code_2_value Code_3 Code_3_value
N1    ABC1        10      ABC2       15       ABC3        6
N2    ABC1        3       NULL      NULL      BAA1        10
N3    ABC4        5       ABC2       11       ADC3        6

and I would like to convert this values into new format:

我想把这些值转换成新的格式:

name    code    value
N1      ABC1      10
N1      ABC2      15
N1      ABC3      6
N2      ABC1      3

I don't know how logically should be (pivot?)

我不知道逻辑应该是怎样的(轴心?)

2 个解决方案

#1


5  

Query -

查询-

DECLARE @t TABLE (
    name VARCHAR(50) PRIMARY KEY,
    Code_1 VARCHAR(50),
    Code_1_value INT,
    Code_2 VARCHAR(50),
    Code_2_value INT,
    Code_3 VARCHAR(50),
    Code_3_value INT
)
INSERT INTO @t
VALUES
    ('N1', 'ABC1', 10, 'ABC2', 15  , 'ABC3', 6),
    ('N2', 'ABC1', 3 , NULL,   NULL, 'BAA1', 10),
    ('N3', 'ABC4', 5 , 'ABC2', 11  , 'ADC3', 6)

SELECT name, code, value
FROM @t
CROSS APPLY (
    VALUES (Code_1, Code_1_value), (Code_2, Code_2_value),  (Code_3, Code_3_value)
) t2 (code, value)
WHERE code IS NOT NULL

Output -

输出-

name     code     value
-------- -------- -----------
N1       ABC1     10
N1       ABC2     15
N1       ABC3     6
N2       ABC1     3
N2       BAA1     10
N3       ABC4     5
N3       ABC2     11
N3       ADC3     6

Post about UNPIVOT -

篇关于透视,

http://blog.devart.com/is-unpivot-the-best-way-for-converting-columns-into-rows.html

http://blog.devart.com/is-unpivot-the-best-way-for-converting-columns-into-rows.html

#2


2  

This can also be done with a UNION

这也可以用一个结合来完成。

SELECT name,code_1 as code,code_1_value as value from yourTable
UNION ALL
SELECT name,code_2 as code,code_2_value as value from yourTable
UNION ALL    
SELECT name,code_3 as code,code_3_value as value from yourTable

#1


5  

Query -

查询-

DECLARE @t TABLE (
    name VARCHAR(50) PRIMARY KEY,
    Code_1 VARCHAR(50),
    Code_1_value INT,
    Code_2 VARCHAR(50),
    Code_2_value INT,
    Code_3 VARCHAR(50),
    Code_3_value INT
)
INSERT INTO @t
VALUES
    ('N1', 'ABC1', 10, 'ABC2', 15  , 'ABC3', 6),
    ('N2', 'ABC1', 3 , NULL,   NULL, 'BAA1', 10),
    ('N3', 'ABC4', 5 , 'ABC2', 11  , 'ADC3', 6)

SELECT name, code, value
FROM @t
CROSS APPLY (
    VALUES (Code_1, Code_1_value), (Code_2, Code_2_value),  (Code_3, Code_3_value)
) t2 (code, value)
WHERE code IS NOT NULL

Output -

输出-

name     code     value
-------- -------- -----------
N1       ABC1     10
N1       ABC2     15
N1       ABC3     6
N2       ABC1     3
N2       BAA1     10
N3       ABC4     5
N3       ABC2     11
N3       ADC3     6

Post about UNPIVOT -

篇关于透视,

http://blog.devart.com/is-unpivot-the-best-way-for-converting-columns-into-rows.html

http://blog.devart.com/is-unpivot-the-best-way-for-converting-columns-into-rows.html

#2


2  

This can also be done with a UNION

这也可以用一个结合来完成。

SELECT name,code_1 as code,code_1_value as value from yourTable
UNION ALL
SELECT name,code_2 as code,code_2_value as value from yourTable
UNION ALL    
SELECT name,code_3 as code,code_3_value as value from yourTable