簡體   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-2025 STACKOOM.COM