简体   繁体   中英

SQL - question on transposing columns to row pivoting on multiple columns

Here's my table

Table A

ID1, COL1, COL2, CODE A1, CODE A2, CODE A3,CODE B1, CODE B2, CODE B3, ..... CODE Z1, CODE Z2, CODE Z3  

Here's the output I want:

ID1, COL1, COL2, CODE A1, CODE A2, CODE A3
ID1, COL1, COL2, CODE B1, CODE B2, CODE B3
    .
    .
    .
ID1, COL1, COL2, CODE Z1, CODE Z2, CODE Z3

Basically I want every occurrence of the combination CODE A1, CODE A2, CODE A3 output as new row along with the first 3 columns that uniquely identifies the record.

Let's say there are 26 occurrences of the combination of three codes, which is 78 columns.

I want to see this record split into 26 records.

Any suggestions you have would be really helpful.

Many databases support the Standard SQL lateral joins, which is the best approach. But a more general approach is simply union all :

select id1, col1, col2, code_a1 as code from t union all
select id1, col1, col2, code_a2 as code from t union all
. . .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM