一、目的
指标需要查询以工作日和周末维度的数据统计,因此需要根据数据的日期判断这一天属于星期几,周一到周五为工作日,周六到周日为周末
二、SQL查询
(一)SQL语句
select day, case when pmod(datediff(create_time,'2023-05-02') + 1,7) = 1 then '周一' when pmod(datediff(create_time,'2023-05-02') + 1,7) = 2 then '周二' when pmod(datediff(create_time,'2023-05-02') + 1,7) = 3 then '周三' when pmod(datediff(create_time,'2023-05-02') + 1,7) = 4 then '周四' when pmod(datediff(create_time,'2023-05-02') + 1,7) = 5 then '周五' when pmod(datediff(create_time,'2023-05-02') + 1,7) = 6 then '周六' else '周日' end as week_time from hurys_dc_dwd.dwd_statistics group by day, case when pmod(datediff(create_time,'2023-05-02') + 1,7) = 1 then '周一' when pmod(datediff(create_time,'2023-05-02') + 1,7) = 2 then '周二' when pmod(datediff(create_time,'2023-05-02') + 1,7) = 3 then '周三' when pmod(datediff(create_time,'2023-05-02') + 1,7) = 4 then '周四' when pmod(datediff(create_time,'2023-05-02') + 1,7) = 5 then '周五' when pmod(datediff(create_time,'2023-05-02') + 1,7) = 6 then '周六' else '周日' end;
(二)SQL语句查询结果
今天是2023年9月26日,周二!
(三)注意点
2023-05-02为任意一天的周二,不是选周一或者周日,这个可以自行验证!