MySQL列转行

一、列转行

mysql 数据库中,我们可能遇到将数据库中某一列的数据(多个值,按照英文逗号分隔),转化为多行数据(即一行转多行),然后join关联表,再转化为一行数据
如下目标数据,需要按照不同版本维护到表格里面,23-01这样的列名需要转换成对于的行的月份时间和对于的数值

car_codemarketversiondisplay_versioncategorysubcategory23-0123-0223-0323-0423-0523-0623-0723-0823-0823-1023-1123-122023
ELECN2023-065+7manufNA0793911491553892033372981006126310715341
ELECN2023-065+7stockNA0793911491554011913372981006126310715341

转化成这样的结果

car_codemarketversiondisplay_versioncategorysubcategorytime_typestat_timegoal_amountetl_time
ELECN2023-065+7manufNAmth2023-0102023-08-30
ELECN2023-065+7manufNAmth2023-02792023-08-30
ELECN2023-065+7manufNAmth2023-033912023-08-30
ELECN2023-065+7manufNAmth2023-041492023-08-30
ELECN2023-065+7manufNAmth2023-051552023-08-30
ELECN2023-065+7manufNAmth2023-063892023-08-30
ELECN2023-065+7manufNAmth2023-072032023-08-30
ELECN2023-065+7manufNAmth2023-083372023-08-30
ELECN2023-065+7manufNAmth2023-092982023-08-30
ELECN2023-065+7manufNAmth2023-1010062023-08-30
ELECN2023-065+7manufNAmth2023-1112632023-08-30
ELECN2023-065+7manufNAmth2023-1210712023-08-30
ELECN2023-065+7manufNAyear202353412023-08-30
ELECN2023-065+7stockNAmth2023-0102023-08-30
ELECN2023-065+7stockNAmth2023-02792023-08-30
ELECN2023-065+7stockNAmth2023-033912023-08-30
ELECN2023-065+7stockNAmth2023-041492023-08-30
ELECN2023-065+7stockNAmth2023-051552023-08-30
ELECN2023-065+7stockNAmth2023-063892023-08-30
ELECN2023-065+7stockNAmth2023-072032023-08-30
ELECN2023-065+7stockNAmth2023-083372023-08-30
ELECN2023-065+7stockNAmth2023-092982023-08-30
ELECN2023-065+7stockNAmth2023-1010062023-08-30
ELECN2023-065+7stockNAmth2023-1112632023-08-30
ELECN2023-065+7stockNAmth2023-1210712023-08-30
ELECN2023-065+7stockNAyear202353412023-08-30

二、实现方式
把上面行转列的数据放在另外一个视图里面,然后源头数据修改或者增加,视图可以直接看到数据变化。语句如下:

select 
    a.car_code as car_code,
    a.market as market,
    if(b.help_topic_id = 12,'year','mth') as time_type,#具体是月的目标还是年的目标
    case when b.help_topic_id = 0 then '2023-01'
        when b.help_topic_id = 1 then '2023-02'
        when b.help_topic_id = 2 then '2023-03'
        when b.help_topic_id = 3 then '2023-04'
        when b.help_topic_id = 4 then '2023-05'
        when b.help_topic_id = 5 then '2023-06'
        when b.help_topic_id = 6 then '2023-07'
        when b.help_topic_id = 7 then '2023-08'
        when b.help_topic_id = 8 then '2023-09'
        when b.help_topic_id = 9 then '2023-10'
        when b.help_topic_id = 10 then '2023-11'
        when b.help_topic_id = 11 then '2023-12' 
        when b.help_topic_id = 12 then '2023' 
        end as stat_time,#具体时间
    a.category as category,#类别
    a.subcategory as subcategory,#子类别
    a.version as version,#版本,每个月版本可能不一样
    SUBSTRING_INDEX(SUBSTRING_INDEX(a.goal_amount, '#', b.help_topic_id +1),'#',-1) as goal_amount,
    CURRENT_TIMESTAMP as etl_time,#etl时间
    a.display_version as display_version
from
(
    select 
        car_code,
        market,
        version,
        display_version,
        category,
        subcategory,
        concat(`23-01` ,'#',`23-02`,'#',`23-03`,'#',`23-04`,'#',`23-05`,'#',`23-06`,'#',`23-07`,'#',`23-08` ,'#',`23-09`,'#',`23-10`,'#',`23-11`,'#',`23-12`,'#',`2023`) as goal_amount # 先把月目标数据拼接起来变成一个字段
    from  eboard_test.imp_eboard_mgt_plan_all
)a join mysql.help_topic b
 where  b.help_topic_id < ( LENGTH( a.goal_amount) - LENGTH(REPLACE ( a.goal_amount, '#', '' )) + 1 );

mysql.help_topic 的作用是对 SUBSTRING_INDEX 函数出来的数据(也就是按照分割符分割出来的)数据连接起来做笛卡尔积。如果 mysql.help_topic 没有权限,可以自己创建一张临时表,用来与要查询的表连接查询。
三、创建视图

create or replace
algorithm = UNDEFINED view `database_name`.`table_name` (
`car_code`,
`market`,
`time_type`,
`stat_time`,
`category`,
`subcategory`,
`version`,
`goal_amount`,
`etl_time`,
`display_version`
) as
select 
    a.car_code as car_code,
    a.market as market,
    if(b.help_topic_id = 12,'year','mth') as time_type,
    case when b.help_topic_id = 0 then '2023-01'
        when b.help_topic_id = 1 then '2023-02'
        when b.help_topic_id = 2 then '2023-03'
        when b.help_topic_id = 3 then '2023-04'
        when b.help_topic_id = 4 then '2023-05'
        when b.help_topic_id = 5 then '2023-06'
        when b.help_topic_id = 6 then '2023-07'
        when b.help_topic_id = 7 then '2023-08'
        when b.help_topic_id = 8 then '2023-09'
        when b.help_topic_id = 9 then '2023-10'
        when b.help_topic_id = 10 then '2023-11'
        when b.help_topic_id = 11 then '2023-12' 
        when b.help_topic_id = 12 then '2023' 
        end as stat_time,
    a.category as category,
    a.subcategory as subcategory,
    a.version as version,
    SUBSTRING_INDEX(SUBSTRING_INDEX(a.goal_amount, '#', b.help_topic_id +1),'#',-1) as goal_amount,
    CURRENT_TIMESTAMP as etl_time,
    a.display_version as display_version
from
(
    select 
        car_code,
        market,
        version,
        display_version,
        category,
        subcategory,
        concat(`23-01` ,'#',`23-02`,'#',`23-03`,'#',`23-04`,'#',`23-05`,'#',`23-06`,'#',`23-07`,'#',`23-08` 
        ,'#',`23-09`,'#',`23-10`,'#',`23-11`,'#',`23-12`,'#',`2023`) as goal_amount
    from  eboard_test.imp_eboard_mgt_plan_all
)a join mysql.help_topic b
 where  b.help_topic_id < ( LENGTH( a.goal_amount) - LENGTH(REPLACE ( a.goal_amount, '#', '' )) + 1 );```

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值