繁体   English   中英

如何将列中的字符串划分为redshift中的列名

[英]how to divide string in a column to be a column name in redshift

我在下面构建了这个查询,以便获得每条路线的总体积、sla_min 以及 sla_status。

sla_status 是使用 case when 语法来计算的,以获得over slameet sla

with data_manifest as (
select no,
       concat(concat(origin,'-'),destination) as route_city, 
       sla_min,
       case
           when status>0 and datediff(day, sla_max_date_time_internal, last_valid_tracking_date_time) > 0 then 'OVER SLA'
           when status=0 and datediff(day, sla_max_date_time_internal, current_date) > 0 then 'OVER SLA' else 'MEET SLA'
        end as status_sla
from data
where trunc(tgltransaksi::date) between ('30 January,2023') and ('9 February,2023')
), data_vol as (
select
    route_city,
    count(distinct no) as volume,
    status_sla,
    sla_min,
from data_manifest
group by route_city, status_sla, sla_min
)

查询结果如下:

route_city     vol      status_sla      sla_min
A - B          20        MEET SLA          2
A - B          40        OVER SLA          2
B - C          30        MEET SLA          1
B - C          30        OVER SLA          1

我的问题是如何将MEET SLAOVER SLA拆分为列名,以便结构如下所示:

route_city    MEET SLA   OVER SLA   total_vol    sla_min
A - B          20           40         60           2
B - C          30           30         60           1 

我应该如何编写查询以在 redshift 中获得所需的结果?

先感谢您

没有看到您的输入数据,不清楚您到底需要什么,但这是一个镜头。

您需要停止按 status_sla 分组并计算 status_sla 的每个值的数量。

with data_manifest as (
select no,
       concat(concat(origin,'-'),destination) as route_city, 
       sla_min,
       case
           when status>0 and datediff(day, sla_max_date_time_internal, last_valid_tracking_date_time) > 0 then 'OVER SLA'
           when status=0 and datediff(day, sla_max_date_time_internal, current_date) > 0 then 'OVER SLA' else 'MEET SLA'
        end as status_sla
from data
where trunc(tgltransaksi::date) between ('30 January,2023') and ('9 February,2023')
), data_vol as (
select
    route_city,
    count(distinct no) as volume,
    count(distinct decode(status_sla, 'MEET SLA', no, NULL)) as meet_sla,
    count(distinct decode(status_sla, 'OVER SLA', no, NULL)) as over_sla,
    sla_min,
from data_manifest
group by route_city, sla_min
)

还有其他方法可以对边缘情况进行投注。 不知道这些是什么导致了这种最小变化的方法。

以上代码未经测试。

暂无
暂无

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

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