从Bigquery中的时间戳中提取日期:一种更好的方法

时间:2017-08-29 07:50:13

标签: timestamp google-bigquery standard-sql

向Bigquery大师提出的一个简短问题。

以下是使用standardSQL

从Bigquery中的时间戳中提取日期的两种方法
#standardSQL
#1
DATE(TIMESTAMP_MILLIS(CAST((timestamp) AS INT64)))
#2
EXTRACT(DATE FROM TIMESTAMP_MILLIS(timestamp))

哪一个更可取,为什么?谢谢!

1 个答案:

答案 0 :(得分:6)

这真的取决于个人喜好;一个并不优于另一个,因为它们具有相同的语义和性能。支持使用EXTRACT的论据是,如果要在选择列表中提取其他日期/时间部分,则会反映它们。例如:

SELECT
  EXTRACT(DATE FROM TIMESTAMP_MILLIS(timestamp)) AS date,
  EXTRACT(ISOYEAR FROM TIMESTAMP_MILLIS(timestamp)) AS iso_year,
  EXTRACT(ISOWEEK FROM TIMESTAMP_MILLIS(timestamp)) AS iso_week
FROM YourTable;

与:相比:

SELECT
  DATE(TIMESTAMP_MILLIS(timestamp)) AS date,
  EXTRACT(ISOYEAR FROM TIMESTAMP_MILLIS(timestamp)) AS iso_year,
  EXTRACT(ISOWEEK FROM TIMESTAMP_MILLIS(timestamp)) AS iso_week
FROM YourTable;