我有一张桌子,我只是在重复50年的日期。
使用周年度(“日期”) -> week_no_in_this_year的值。
我想创建一个使用(week_no_in_this_year)的专栏,它应该是唯一的一周。把它命名为-> week_id
这应该是连接的Year+two_digit_week_no_in_this_year+Some_number(to使这个id唯一的一个星期)。我试过如下:
作为week_no_in_this_year<10(week_no_in_this_year<10,concat(0,week_no_in_this_year),week_no_in_this_year),'2')作为week_id。
但在下面的场景中,我要面对几个日期的问题:
SELECT weekofyear("2019-01-01") ;
SELECT concat(concat("2019",IF(1<10, concat(0,1),1)),'2') AS week_id;
Expected Result: 2019012
SELECT weekofyear("2019-12-31");
SELECT concat(concat("2019",IF(1<10, concat(0,1),1)),'2') AS week_id;
Expected Result: 2020012发布于 2018-03-12 19:07:05
这种情况只有在给定年份结束时(即12月31日)的年份之间出现分裂,而周数则转到下一年时才发生。如果我们为这个案子设定一个条件,我们就能得到你所期望的。
右函数与substr (,-n)相同。
SELECT DTE as Date,
CONCAT(IF(MONTH(DTE)=12 and WEEKOFYEAR(DTE)=1, year(DTE)+1, year(DTE)),
SUBSTR(CONCAT('0', WEEKOFYEAR(DTE)), -2), '2') as weekid
FROM tbl;
Result:
Date WeekId
2019-01-01 2019012
2019-11-01 2019442
2019-12-31 2020012发布于 2018-03-12 21:19:28
一种方法是使用UDF。创建python脚本并将其推送到HDFS
mypy.py
import sys
import datetime
for line in sys.stdin:
line = line.strip()
(y,m,d) = line.split("-")
d = datetime.date(int(y),int(m),int(d)).isocalendar()
print str(d[0])+str(d[1])在蜂巢里
add file hdfs:/user/cloudera/mypy.py;
select transform("2019-1-1") using "python mypy.py" as (week_id);
INFO : OK
+----------+--+
| week_id |
+----------+--+
| 20191 |
+----------+--+
select transform("2019-12-30") using "python mypy.py" as (week_id)
+----------+--+
| week_id |
+----------+--+
| 20201 |
+----------+--+
1 row selected (33.413 seconds)https://stackoverflow.com/questions/49235358
复制相似问题