计算指定时间所在的季度数及当季的第一天和最后一天

-- 指定时间所在的季度的第一天

1 -- oracle写法
2 select trunc(to_date('2018-08-02'), 'Q') from dual;

 

SQL实现:

 1 -- hive写法
 2 select
 3      day
 4     ,floor(month(day)/3) + 1                                                         as season_error
 5     ,ceil(month(day)/3)                                                              as season_num
 6     ,lpad(ceil(month(day)/3),2,0)                                                    as season_num
 7     ,to_date(concat(year(day),'-',lpad(ceil(month(day)/3) * 3 -2,2,0),'-01'))        as season_of_first_day
 8     ,last_day(to_date(concat(year(day),'-',lpad(ceil(month(day)/3) * 3,2,0),'-01'))) as season_of_last_day
 9     ,case
10         when month(day) <= 3 then to_date(concat(year(day),'-01-01'))
11         when month(day) <= 6 then to_date(concat(year(day),'-04-01'))
12         when month(day) <= 9 then to_date(concat(year(day),'-07-01'))
13         when month(day) <=12 then to_date(concat(year(day),'-10-01'))
14     end as season_of_first_day
15 from (
16     select '2018-01-02' as day union all
17     select '2018-02-02' as day union all
18     select '2018-03-02' as day union all
19     select '2018-04-02' as day union all
20     select '2018-05-02' as day union all
21     select '2018-06-02' as day union all
22     select '2018-07-02' as day union all
23     select '2018-08-02' as day union all
24     select '2018-09-02' as day union all
25     select '2018-10-02' as day union all
26     select '2018-11-02' as day union all
27     select '2018-12-02' as day
28 ) t1
29 ;

 

+-------------+---------------+-------------+-------------+----------------------+---------------------+----------------------+--+
|     day     | season_error  | season_num  | season_num  | season_of_first_day  | season_of_last_day  | season_of_first_day  |
+-------------+---------------+-------------+-------------+----------------------+---------------------+----------------------+--+
| 2018-01-02  | 1             | 1           | 01          | 2018-01-01           | 2018-03-31          | 2018-01-01           |
| 2018-02-02  | 1             | 1           | 01          | 2018-01-01           | 2018-03-31          | 2018-01-01           |
| 2018-03-02  | 2             | 1           | 01          | 2018-01-01           | 2018-03-31          | 2018-01-01           |
| 2018-04-02  | 2             | 2           | 02          | 2018-04-01           | 2018-06-30          | 2018-04-01           |
| 2018-05-02  | 2             | 2           | 02          | 2018-04-01           | 2018-06-30          | 2018-04-01           |
| 2018-06-02  | 3             | 2           | 02          | 2018-04-01           | 2018-06-30          | 2018-04-01           |
| 2018-07-02  | 3             | 3           | 03          | 2018-07-01           | 2018-09-30          | 2018-07-01           |
| 2018-08-02  | 3             | 3           | 03          | 2018-07-01           | 2018-09-30          | 2018-07-01           |
| 2018-09-02  | 4             | 3           | 03          | 2018-07-01           | 2018-09-30          | 2018-07-01           |
| 2018-10-02  | 4             | 4           | 04          | 2018-10-01           | 2018-12-31          | 2018-10-01           |
| 2018-11-02  | 4             | 4           | 04          | 2018-10-01           | 2018-12-31          | 2018-10-01           |
| 2018-12-02  | 5             | 4           | 04          | 2018-10-01           | 2018-12-31          | 2018-10-01           |
+-------------+---------------+-------------+-------------+----------------------+---------------------+----------------------+--+

 

2. 创建相应的日期维表可以实现

3. 通过shell可以实现

inc_start='20181112'
IncStart=$inc_start
IncStartYear=`echo ${IncStart:0:4}`;
IncStartMonth=`echo ${IncStart:4:2}`;
IncStartQuarter=${IncStartYear}"Q"$(((10#${IncStartMonth}-1)/3+1));
echo ${IncStartQuarter}

 

转载于:https://www.cnblogs.com/chenzechao/p/9449933.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值