记一道有趣的sql题

有一张运单表: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为否)。需求为:

求连续俩周 严重破损的客户,丢失的客户、取超时的客户。

思路:

步骤一:

求每天严重破损、取超时的客户

sql 复制代码
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
),

第二步,求本周与上周的相关指标

sql 复制代码
 /*客户连续俩周的相关指标*/
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
)

第三部,将本周与上周的指标打平成一行

sql 复制代码
 /*客户连续俩周的相关指标*/
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
)

第四步:计算结果:

sql 复制代码
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
相关推荐
honder试试4 小时前
Centos7从0-1安装部署Clickhouse验证与Mysql实时同步
数据库·mysql·clickhouse
VX:Fegn08954 小时前
计算机毕业设计|基于springboot + vue心理健康管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
Shingmc34 小时前
MySQL表的约束
数据库·mysql
sugarzhangnotes4 小时前
应用服务OOM引发GC异常,导致Redis请求超时失败的问题分析与解决
数据库·redis·测试工具
SelectDB4 小时前
面向 Agent 的高并发分析:Doris vs. Snowflake vs. ClickHouse
数据库·apache·agent
alien爱吃蛋挞4 小时前
【JavaEE】Spring Boot日志
java·数据库·spring boot
zjeweler4 小时前
redis tools gui ---Redis图形化漏洞利用工具
数据库·redis·web安全·缓存
Leon-Ning Liu4 小时前
Oracle 19c RAC ASM 密码文件恢复方案四:创建新密码文件覆盖恢复
数据库·oracle
思成不止于此4 小时前
【MySQL 零基础入门】DCL 核心语法全解析:用户管理与权限控制篇
数据库·笔记·sql·学习·mysql
武子康5 小时前
Java-192 深入拆解 EVCache 内部原理:Memcached 架构、Slab 分配与 LRU 过期机制全解析
数据库·redis·缓存·架构·memcached·guava·evcache