简体   繁体   English

mySQL 如何取消透视动态列

[英]mySQL how to unpivot dynamic columns

I have the following table below:我有下表:

Table name: STAT表名:STAT

Region   M07  M08 M09 
---------------------
P1       0    1   0    
P2       0    0   0   
P3       2    0   0    
P4       0    0   0     
P5       0    0   0     
P6       0    0   0   
P7       9    0   3  

I would like to UNPIVOT the months columns, which contain numerical values.我想 UNPIVOT 包含数值的月份列。 This Months column headers are DYNAMIC and will be changing, but the total number of columns will remain the same.这个 Months 列标题是动态的,并且会发生变化,但列的总数将保持不变。

Expected Result预期结果

---------------------
Region   Month  Qty
---------------------
P1      M07    0
P1      M08    1
P1      M09    0
P2      M07    0
P2      M08    0
P2      M09    0

It would be great if I can have some help writing this SQL statement to UNPIVOT and then also consider the dynamic nature of the numerical columns.如果我能得到一些帮助,将这个 SQL 语句写入 UNPIVOT,然后还要考虑数字列的动态性质,那就太好了。 It should written in mySQL.它应该写在 mySQL 中。

I have the following table below:我有下表:

Table name: STAT表名:STAT

Region   M07  M08 M09 
---------------------
P1       0    1   0    
P2       0    0   0   
P3       2    0   0    
P4       0    0   0     
P5       0    0   0     
P6       0    0   0   
P7       9    0   3  

I would like to UNPIVOT the months columns, which contain numerical values.我想对包含数值的月份列进行 UNPIVOT。 This Months column headers are DYNAMIC and will be changing, but the total number of columns will remain the same.这个月份的列标题是动态的并且会发生变化,但列的总数将保持不变。

Expected Result预期结果

---------------------
Region   Month  Qty
---------------------
P1      M07    0
P1      M08    1
P1      M09    0
P2      M07    0
P2      M08    0
P2      M09    0

It would be great if I can have some help writing this SQL statement to UNPIVOT and then also consider the dynamic nature of the numerical columns.如果我能帮助我将此 SQL 语句写入 UNPIVOT,然后再考虑数值列的动态特性,那就太好了。 It should written in mySQL.它应该写在 mySQL 中。

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

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