查询上周五到本周四以及同比时间

查了好多文章,都不满足需求,

要求本周为上个周五到昨天,不含今天;

如果今天是周三,是上周五到本周二;

如果今天是周日,是这周五到周六

 如果今天正好是周五,是上周五到本周四

最后自己研究了下,验证了一个月的日期,发现是OK的

 

select 
dd.date,
date_format(dd.date,'%w'),
if(date_format(dd.date,'%w')=6,SUBDATE(dd.date,1),subdate(dd.date,(date_format(dd.date,'%w')+2))) AS '上周五',
subdate(dd.date,1) as '昨天',
subdate(if(date_format(dd.date,'%w')=6,SUBDATE(dd.date,1),subdate(dd.date,(date_format(dd.date,'%w')+2))),7) AS '上上周五',
subdate(if(date_format(dd.date,'%w')=6,SUBDATE(dd.date,1),subdate(dd.date,(date_format(dd.date,'%w')+2))),1) as '上周四',


subdate(if(date_format(dd.date,'%w')=6,SUBDATE(dd.date,1),subdate(dd.date,(date_format(dd.date,'%w')+2))),14) AS '上上上周五',
subdate(if(date_format(dd.date,'%w')=6,SUBDATE(dd.date,1),subdate(dd.date,(date_format(dd.date,'%w')+2))),8) AS '上上周四',

subdate(if(date_format(dd.date,'%w')=6,SUBDATE(dd.date,1),subdate(dd.date,(date_format(dd.date,'%w')+2))),21) AS '上上上上周五',
subdate(if(date_format(dd.date,'%w')=6,SUBDATE(dd.date,1),subdate(dd.date,(date_format(dd.date,'%w')+2))),15) AS '上上上周四'
from date_dim dd
where dd.date>='2022-03-01'

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值