Athenaで日付から曜日を取得したい
Athenaのprestoベースなのでdate_formatのprestoのドキュメントを参照すると
%w Day of the week (0 .. 6), where Sunday is the first day of the week 3
とあるためたとえば2021/04/25の日曜日の番号を取得するには
select date_format(timestamp '2021-04-25', '%w')
で取得可能と思われる。
しかし、実際に実行してみると
INVALID_FUNCTION_ARGUMENT: %w not supported in date format string
とエラーが発生される。Athenaでは%wがサポートされていないようです。
day_of_weekの利用
day_of_weekを利用すると曜日番号が取得可能です。
select day_of_week(timestamp '2021-04-25')
これで番号が取得可能なのですが、day_of_weekは
Returns the ISO day of the week from x. The value ranges from 1 (Monday) to 7 (Sunday).
とあるため月曜から土曜日は1~6でdate_formatと同じなのですが、日曜日だけ0ではなく7が返ってきてしまいます。
従って0~6で取得したい場合は
select if (day_of_week(timestamp '2021-04-25') = 7, 0, day_of_week(timestamp '2021-04-25'))
とif文で7の場合に0に置き換えます。
他にいいやり方あったら教えて下さい!