trino / presto 列转行 取JSON


select
    id
    ,json_extract_scalar(json_item,'$.user_id') as user_id
    ,json_extract_scalar(json_item,'$.url')     as user_id
from (
    select
         A.id
        ,concat('{',replace(replace(B.json,'[{',''),'}]',''),'}') as json_item
    FROM (
        select
             t1.id
            ,t1.image_file
        from ods_zj_q_closed_water_test_photo_requirement t1
        where db_name in ('db_name')
        limit 2
    ) A
    cross join unnest(split(image_file, '},{')) as B(json)
) t1
;

select
     A.id
    ,try_cast(B.json as int) as json_item
FROM (
    select
         t1.id
        ,t1.str
    from (select 1 as id,'aa,1,2,cc,5' as str ) t1
) A
cross join unnest(split(str, ',')) as B(json)
;

select
     A.id
    ,max(cast(regexp_replace(B.json,'\D','0') as int)) as json_item
FROM (
    select
         t1.id
        ,t1.str
    from (select 1 as id,'aa,1,2,cc,5' as str ) t1
) A
cross join unnest(split(str, ',')) as B(json)
group by a.id
;

posted @ 2022-07-23 20:11  chenzechao  阅读(1434)  评论(0)    收藏  举报