[英]how to divide string in a column to be a column name in redshift
I have built this query below in order to get the total vol by each route, sla_min and also the sla_status.我在下面构建了这个查询,以便获得每条路线的总体积、sla_min 以及 sla_status。
The sla_status is calculated with case when syntax to get the over sla
and also meet sla
sla_status 是使用 case when 语法来计算的,以获得
over sla
并meet 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
)
The query results this:查询结果如下:
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
my question is how can I split the MEET SLA
and OVER SLA
become the column names so the structure would be like this:我的问题是如何将
MEET SLA
和OVER SLA
拆分为列名,以便结构如下所示:
route_city MEET SLA OVER SLA total_vol sla_min
A - B 20 40 60 2
B - C 30 30 60 1
how should I write the query to get the desired result in redshift?我应该如何编写查询以在 redshift 中获得所需的结果?
thank you in advance先感谢您
Not seeing your input data it isn't clear what exactly you need but here's a shot.没有看到您的输入数据,不清楚您到底需要什么,但这是一个镜头。
You need to stop grouping by status_sla and count the number for each value of status_sla.您需要停止按 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
)
There are other ways of doing this that might work betting for the edge cases.还有其他方法可以对边缘情况进行投注。 Not knowing what these are results in this minimal change approach.
不知道这些是什么导致了这种最小变化的方法。
Above code is untested.以上代码未经测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.