有一张运单表:dwd_biz_waybill_td,该表的主键是way_bill_id,并且有如下字段:
way_bill_id(运单表主键),shiping_date(下单日期,时间格式为yyyy-MM-dd),payment_customer_id(付款客户),damaged_degree_type(是否严重破损,为枚举值。1为是,0为否),is_throw_away(是否丢失,为枚举值。1为是,0为否),is_pickup_overtime(是否取超时,为枚举值。1为是,0为否)。需求为:
求连续俩周 严重破损的客户,丢失的客户、取超时的客户。最终结果表的字段:
payment_customer_id,is_serious_damage_tw /*是否【连续2周】严重破损*/,is_serious_damage_tw /*是否【连续2周】丢失*/,is_serious_damage_tw, /*是否【连续2周】取超时*/
注意:这里的连续是指符合要求的周要连续。比如本周符合条件以及上周符合条件。
思路:
方式一:
步骤一:
求每天严重破损、取超时的客户
with v_customer_votes_base_data as (
select
waybill_td.payment_customer_id,
waybill_td.shiping_date,
CASE
when shiping_date>= date_format(date_trunc('week',current_date),'%Y-%m-%d') then 'cur_week'
when cast(date_add('day',-7,cast(date_format(date_trunc('week',current_date),'%Y-%m-%d') as date )) as varchar)<=shiping_date
and shiping_date<cast(date_format(date_trunc('week',current_date),'%Y-%m-%d') as varchar) then 'last_week'
else null
end as shiping_date_week,
sum(if(damaged_degree_type = 1,1,0)) as payment_serious_damage_votes , /**严重破损**/
sum(if(is_throw_away = 1,1,0)) as payment_throw_away_votes , /**丢失票(排除退栏单)**/
sum(if(is_pickup_overtime = 1,1,0)) as payment_order_pickup_overdue_votes ,/*订单取超时数*/
from dwd_biz_waybill_td as waybill_td
where waybill_td.shiping_date >= cast(date_add('day',-7,cast(date_format(date_trunc('week',current_date),'%Y-%m-%d') as date )) as varchar)
and waybill_td.shiping_date<= date_format(current_date,'%Y-%m-%d')
group by waybill_td.payment_customer_id,waybill_td.shiping_date
),
第二步,求本周与上周的相关指标
/*客户连续俩周的相关指标*/
v_customer_votes_result as (
select payment_customer_id,
shiping_date_week, /*本周还是上周*/
sum(payment_serious_damage_votes) as payment_serious_damage_votes , /*严重破损*/
sum(payment_throw_away_votes) as payment_throw_away_votes, /*丢失票(排除退栏单)*/
sum(payment_order_pickup_overdue_votes) as payment_order_pickup_overdue_votes, /*订单取超时数*/
sum(payment_inner_overdue_votes) as payment_inner_overdue_votes /*内因超时票(内因)*/
from v_customer_votes_base_data
where shiping_date_week is not null
group by payment_customer_id,shiping_date_week
)
第三部,将本周与上周的指标打平成一行文章来源:https://www.toymoban.com/news/detail-621516.html
/*客户连续俩周的相关指标*/
v_customer_votes_result_two_week as (
select payment_customer_id,
sum(if(shiping_date_week='cur_week' and payment_serious_damage_votes>0,
1,
0)
) as payment_serious_damage_votes,
sum(if(shiping_date_week='last_week' and payment_serious_damage_votes>0,
1,
0)
) as payment_serious_damage_votes_last_week,
sum(if(shiping_date_week='cur_week' and payment_throw_away_votes>0,
1,
0)
) as payment_throw_away_votes,
sum(if(shiping_date_week='last_week' and payment_throw_away_votes>0,
1,
0)
) as payment_throw_away_votes_last_week,
sum(if(shiping_date_week='cur_week' and payment_order_pickup_overdue_votes>0,
1,
0)
) as payment_order_pickup_overdue_votes,
sum(if(shiping_date_week='last_week' and payment_order_pickup_overdue_votes>0,
1,
0)
) as payment_order_pickup_overdue_votes_last_week
from v_customer_votes_result
group by payment_customer_id
)
第四步:计算结果:文章来源地址https://www.toymoban.com/news/detail-621516.html
select
if(payment_serious_damage_votes>0 and payment_serious_damage_votes_last_week>0,1,0) as is_serious_damage_tw, /*是否【连续2周】严重破损*/
if(payment_throw_away_votes>0 and payment_throw_away_votes_last_week>0,1,0) as is_serious_damage_tw /*是否【连续2周】丢失*/
if(payment_order_pickup_overdue_votes>0 and payment_order_pickup_overdue_votes_last_week>0,1,0) as is_serious_damage_tw, /*是否【连续2周】取超时*/
from v_customer_votes_result_two_week
方式二:
/*客户连续俩周的相关指标*/
with v_customer_votes_result_two_week as (
select payment_customer_id,
sum(if(shiping_date>= date_format(date_trunc('week',current_date),'%Y-%m-%d') and damaged_degree_type = 1,
1,
0)
) as payment_serious_damage_votes, /**本周严重破损**/
sum(if(cast(date_add('day',-7,cast(date_format(date_trunc('week',current_date),'%Y-%m-%d') as date )) as varchar)<=shiping_date
and shiping_date<cast(date_format(date_trunc('week',current_date),'%Y-%m-%d') as varchar) and damaged_degree_type = 1,
1,
0)
) as payment_serious_damage_votes_last_week, /**上周严重破损**/
sum(if(shiping_date>= date_format(date_trunc('week',current_date),'%Y-%m-%d') and is_throw_away = 1,
1,
0)
) as payment_throw_away_votes, /**本周丢失票(排除退栏单)**/
sum(if(cast(date_add('day',-7,cast(date_format(date_trunc('week',current_date),'%Y-%m-%d') as date )) as varchar)<=shiping_date
and shiping_date<cast(date_format(date_trunc('week',current_date),'%Y-%m-%d') as varchar) and is_throw_away = 1,
1,
0)
) as payment_throw_away_votes_last_week, /**上周丢失票(排除退栏单)**/
sum(if(shiping_date>= date_format(date_trunc('week',current_date),'%Y-%m-%d') and is_pickup_overtime = 1,
1,
0)
) as payment_order_pickup_overdue_votes, /*本周订单取超时数*/
sum(if(cast(date_add('day',-7,cast(date_format(date_trunc('week',current_date),'%Y-%m-%d') as date )) as varchar)<=shiping_date
and shiping_date<cast(date_format(date_trunc('week',current_date),'%Y-%m-%d') as varchar) and is_pickup_overtime = 1,
1,
0)
) as payment_order_pickup_overdue_votes_last_week /*上周订单取超时数*/
from v_customer_votes_result
group by payment_customer_id
)
select
if(payment_serious_damage_votes>0 and payment_serious_damage_votes_last_week>0,1,0) as is_serious_damage_tw, /*是否【连续2周】严重破损*/
if(payment_throw_away_votes>0 and payment_throw_away_votes_last_week>0,1,0) as is_serious_damage_tw /*是否【连续2周】丢失*/
if(payment_order_pickup_overdue_votes>0 and payment_order_pickup_overdue_votes_last_week>0,1,0) as is_serious_damage_tw, /*是否【连续2周】取超时*/
from v_customer_votes_result_two_week
到了这里,关于记一道有趣的sql题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!