hive SQL查询将具有多个状态更改日期列的单行转换为具有状态和时间戳的多行

bt1cpqcv  于 5个月前  发布在  Hive
关注(0)|答案(1)|浏览(57)

我的输入数据如下:
| story_id| sched状态|故事被修饰ts|故事定义|新闻中心|story_cmpl_ts|
| --|--|--|--|--|--|
| 1 |完成|2023-01-01 2023-01-01| 2023-01-13 2023-01-13 2023-01-13| 2023-01-20 2023-01-20 2023-01-20| 2023-01-23 - 2023-01-23|
| 2 |inprogress| 2023-01-01 2023-01-01| 2023-01- 10 2023-01-10 2023-01-10| 2023-01-30 - 2023-01-30||
我希望我的输出是这样的:
根据时间戳的可用性,我们需要更新输出中的阶段,总共有4个阶段(Groomed,defined,inprogress和completed)。
| story_id| sched状态|最后_up_ts|
| --|--|--|
| 1 |培养|2023-01-01 2023-01-01|
| 1 |定义|2023-01-13 2023-01-13 2023-01-13|
| 1 |inprogress| 2023-01-20 2023-01-20 2023-01-20|
| 1 |完成|2023-01-23 - 2023-01-23|
| 2 |培养|2023-01-01 2023-01-01|
| 2 |定义|2023-01- 10 2023-01-10 2023-01-10|
| 2 |inprogress| 2023-01-30 - 2023-01-30|
你能帮帮我吗?

hof1towb

hof1towb1#

在mysql中没有直接的反透视方法,可以通过下面的方法来实现:

select story_id, sched_state, last_up_ts
from (
    select t.story_id,
      c.sched_state,
      case c.sched_state
        when 'Groomed' then story_being_groomed_ts
        when 'defined' then story_def_ts
        when 'inprogress' then story_in_prgrs_ts
        when 'completed' then story_cmpl_ts
      end as last_up_ts
    from mytable t
    cross join
    (
      select 'Groomed' as sched_state
      union all select 'defined'
      union all select 'inprogress'
      union all select 'completed'
    ) c
) sub
where last_up_ts is not null 
ORDER BY story_id, sched_state, last_up_ts

字符串

相关问题