简体   繁体   English

SQL - 关于在多列上将列转置为行的问题

[英]SQL - question on transposing columns to row pivoting on multiple columns

Here's my table这是我的桌子

Table A表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:这是我想要的 output:

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.基本上,我希望每次出现组合 CODE A1、CODE A2、CODE A3 output 作为新行以及唯一标识记录的前 3 列。

Let's say there are 26 occurrences of the combination of three codes, which is 78 columns.假设三个代码的组合出现了 26 次,即 78 列。

I want to see this record split into 26 records.我想看到这条记录分成 26 条记录。

Any suggestions you have would be really helpful.您的任何建议都会非常有帮助。

Many databases support the Standard SQL lateral joins, which is the best approach.许多数据库支持标准 SQL 横向连接,这是最好的方法。 But a more general approach is simply union all :但更通用的方法是简单地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
. . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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