1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Hive関数で週番号を取得

Last updated at Posted at 2016-09-15

困ったこと

Hiveで週次集計をしたい。
、、、が、わざわざ日付情報を7で割って、、というのがちょっと面倒だし、
ありあわせの関数でナントカならんもんか、と思った。
(´・ω・`)

方針

週番号を付加して、そいつでgroup byすればいいじゃまいか!
(※) 「週番号」ってこういうやつ↓

その年の最初の週をWeek 1とし、以降の週を連番で表示するウィークカレンダー方式での週の指定は、ヨーロッパやアメリカでよく使われますが、1週間のはじまりを「月曜日」とするか「日曜日」にするかという違いと、1月1日が含まれる週を必ずWeek 1とするかどうかで、数え方に違いがあります。
(http://www.toishi.info/email/week_calendar_2016.html より引用)

解決策

weekofyear関数を使おう!

フォーマット

weekofyear("yyyy-mm-dd")

に対して、週番号を返してくれる。

select weekofyear("1970-11-01")
---44

何曜始まりかを確認

select weekofyear("2016-09-11")
---36 <- 日曜
select weekofyear("2016-09-12")
---37 <- 月曜

ということで、月曜始まり!

年末年始

例えば、2015年末は2015/12/29が月曜で同じ週に年が明けます(2016/1/1は木曜)

select weekofyear('2015-12-28')
 --- 53 <- 月曜

select weekofyear('2016-01-01')
 --- 53 <- 木曜
 
select weekofyear('2016-01-03')
 --- 53 <- 日曜

select weekofyear('2016-01-04')
 --- 1 <- 月曜

ということで、1/1が月曜でない(今回のケースのような場合)は、昨年末の週番号を引き継ぎ、
次の月曜が週番号1となるものを採用!

日付のフォーマットが異なる場合

データの持ち方によっては日付がyyyy-mm-ddでない場合もある。
そんな時はfrom_unixtime, unix_timestampで無理矢理形式を揃えれば出力される。

select weekofyear(from_unixtime(unix_timestamp('20151228','yyyymmdd'), 'yyyy-mm-dd'))
 --- 53

参考サイト

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?