14
5

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.

【SQL】AGE関数の無いBigQueryで年齢を出力する二つの方法

Last updated at Posted at 2019-03-04

#概要#

全ての人間は2種類に分けることができる。
        BigQueryで年齢を出力出来る者と、そうでない者
                    @74kenshiro(1988~2130年)

名もなき初心者エンジニアがBigQueryでただ年齢を出力するだけの記事です。

目次
1.年齢を導き出すそもそもの計算式
2.CASTとREPLACEで変換し、年齢を出力する方法
3.FORMAT_DATE関数でオシャレに変換し、出力する方法
4.まとめ
##この記事で分かること##
BigQueryでのDATE型から年齢を出力する方法

##データ概要
元データ:とある会社の社員データ「staff_list」
image.png
年齢を導き出すbirth_dateはDATE型である。

##1.年齢を導き出すそもそもの計算式##
image.png

例:2019年3月1日時点での1988年3月28日生まれの人の年齢
image.png

##2.CASTとREPLACEで変換し、年齢を出力する方法

①birth_dateをDATE型から文字型STRINGに変換

CAST(birth_date AS string) AS birth_date01_str

②ハイフンを除外 ※TRIM関数では出来ないのでREPLACE関数でハイフンを無にする。

REPLACE(CAST(birth_date AS string),'-','') AS birth_date02_str

③整数型INT64に変換

CAST(REPLACE(CAST(birth_date AS string),'-','') AS int64) AS birth_date03_int64,

④本日の日付けと差し引き、10,000で割る計算(本日の日付けCURRENT_DATEも同じ処理をしておく)

CAST(REPLACE(CAST(current_date AS string),'-','') AS int64) AS current_date_int64,
(CAST(REPLACE(CAST(current_date AS string),'-','') AS int64) - CAST(REPLACE(CAST(birth_date AS string),'-','') AS int64)) / 10000 AS age_float,

⑤整数型INT64に変換して小数点以下切り捨て

 CAST((CAST(REPLACE(CAST(current_date AS string),'-','') AS int64) - CAST(REPLACE(CAST(birth_date AS string),'-','') AS int64)) / 10000 AS int64) AS age
SELECT
  id,
  birth_date,
  CAST(birth_date AS string) AS birth_date01_str,
  REPLACE(CAST(birth_date AS string),'-','') AS birth_date02_str,
  CAST(REPLACE(CAST(birth_date AS string),'-','') AS int64) AS birth_date03_int64,
  CAST(REPLACE(CAST(current_date AS string),'-','') AS int64) AS current_date_int64,
  (CAST(REPLACE(CAST(current_date AS string),'-','') AS int64) - CAST(REPLACE(CAST(birth_date AS string),'-','') AS int64)) / 10000 AS age_float,
  CAST((CAST(REPLACE(CAST(current_date AS string),'-','') AS int64) - CAST(REPLACE(CAST(birth_date AS string),'-','') AS int64)) / 10000 AS int64) AS age
FROM
  `Dataset.staff_list`

OUTPUT
実際は計算するクエリの最終行だけでいいが、以下のように段階的に処理している!!

image.png

###ゴリ押し感はぬぐえないが無事出力出来た。
DATE型から年齢を出力するクエリ

  CAST((CAST(REPLACE(CAST(current_date AS string),'-','') AS int64) - CAST(REPLACE(CAST(birth_date AS string),'-','') AS int64)) / 10000 AS int64) AS age

##3.FORMAT_DATE関数でオシャレに変換し、出力する方法
CASTREPLACEを使ってした文字型化とハイフン除去が、FORMAT_DATEなら一発で出来る。

SELECT
  id,
  birth_date,
  --2のやり方で、DATE型➡文字型➡ハイフン除去
  REPLACE(CAST(birth_date AS string),'-','') AS birth_date_str_CR,
  --FORMAT_DATEを使ったクエリ
  FORMAT_DATE('%Y%m%d',birth_date)AS birth_date_str_F
FROM
  `Dataset.staff_list`

image.png

これに計算を追加するとこうなる。
3行目:2のやり方
4行目:FORMAT_DATEでのやり方

SELECT
  id,
  birth_date,
  CAST((CAST(REPLACE(CAST(current_date AS string),'-','') AS int64) - CAST(REPLACE(CAST(birth_date AS string),'-','') AS int64)) / 10000 AS int64) AS age_CR,
  CAST((CAST(FORMAT_DATE('%Y%m%d', CURRENT_DATE()) AS INT64) - CAST(FORMAT_DATE('%Y%m%d', birth_date ) AS INT64)) / 10000 AS int64) AS age_F
FROM
  `Dataset.staff_list`

image.png

##4.まとめ
以上が年齢を出力する二つの方法でした。
どっちでやるかは君次第。

CAST((CAST(REPLACE(CAST(current_date AS string),'-','') AS int64) - CAST(REPLACE(CAST(birth_date AS string),'-','') AS int64)) / 10000 AS int64) AS age_CR,
CAST((CAST(FORMAT_DATE('%Y%m%d', CURRENT_DATE()) AS INT64) - CAST(FORMAT_DATE('%Y%m%d', birth_date ) AS INT64)) / 10000 AS int64) AS age_F

#おわり

14
5
1

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
14
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?