从Bigquery中的时间戳中提取月份和年份

时间:2018-01-11 12:21:19

标签: google-bigquery

我想从Timestamp列(YYYYMMDD HHMMSS)中提取月份和年份,如果month为1,则应创建一个名为“January”且月份为2的列,它应该创建名为“February”的列。

以下是我尝试但没有运气的查询。

SELECT
  SUM(case when extract(month() from timestamp) = 1 and extract(year() from timestamp) = '2018' then total else 0 end ) as January ,
  SUM(case when extract(month() from timestamp) = 2 and extract(year() from timestamp) = '2018' then total else 0 end ) as February, and so on until December..

FROM x;

5 个答案:

答案 0 :(得分:1)

以下是BigQuery StandardSQL

  
#standardsQL
CREATE TEMP FUNCTION monthValue(ts TIMESTAMP, m INT64, y INT64, value INT64) AS (
  IF(EXTRACT(MONTH FROM ts) = m AND EXTRACT(YEAR FROM ts) = y, value, 0) 
);
SELECT
  SUM(monthValue(timestamp, 1, 2018, total)) AS January,
  SUM(monthValue(timestamp, 2, 2018, total)) AS February,
  SUM(monthValue(timestamp, 3, 2018, total)) AS March,
  SUM(monthValue(timestamp, 4, 2018, total)) AS April,
  SUM(monthValue(timestamp, 5, 2018, total)) AS May,
  SUM(monthValue(timestamp, 6, 2018, total)) AS June,
  SUM(monthValue(timestamp, 7, 2018, total)) AS July,
  SUM(monthValue(timestamp, 8, 2018, total)) AS August,
  SUM(monthValue(timestamp, 9, 2018, total)) AS September,
  SUM(monthValue(timestamp, 10, 2018, total)) AS October,
  SUM(monthValue(timestamp, 11, 2018, total)) AS November,
  SUM(monthValue(timestamp, 12, 2018, total)) AS December
FROM
  `yourproject.yourdataset.yourtable`
  

我们可以在Legacy SQL中执行此操作吗?

for Legacy SQL见下文

#legacySQL
SELECT
  SUM(IF(MONTH(timestamp) = 1 AND YEAR(timestamp) = 2018, total, 0)) AS January,
  SUM(IF(MONTH(timestamp) = 2 AND YEAR(timestamp) = 2018, total, 0)) AS February,
  SUM(IF(MONTH(timestamp) = 3 AND YEAR(timestamp) = 2018, total, 0)) AS March,
  SUM(IF(MONTH(timestamp) = 4 AND YEAR(timestamp) = 2018, total, 0)) AS April,
  SUM(IF(MONTH(timestamp) = 5 AND YEAR(timestamp) = 2018, total, 0)) AS May,
  SUM(IF(MONTH(timestamp) = 6 AND YEAR(timestamp) = 2018, total, 0)) AS June,
  SUM(IF(MONTH(timestamp) = 7 AND YEAR(timestamp) = 2018, total, 0)) AS July,
  SUM(IF(MONTH(timestamp) = 8 AND YEAR(timestamp) = 2018, total, 0)) AS August,
  SUM(IF(MONTH(timestamp) = 9 AND YEAR(timestamp) = 2018, total, 0)) AS September,
  SUM(IF(MONTH(timestamp) = 10 AND YEAR(timestamp) = 2018, total, 0)) AS October,
  SUM(IF(MONTH(timestamp) = 11 AND YEAR(timestamp) = 2018, total, 0)) AS November,
  SUM(IF(MONTH(timestamp) = 12 AND YEAR(timestamp) = 2018, total, 0)) AS December
FROM [yourproject:yourdataset.yourtable]  

注意:BigQuery团队非常推荐migrate to Standard SQL

答案 1 :(得分:1)

您可以尝试:

SELECT CONCAT(CAST(EXTRACT(YEAR FROM DATE (OrderDate)) AS STRING),"-",CAST(EXTRACT(MONTH FROM DATE (OrderDate)) AS STRING)) AS yyyy_mm

答案 2 :(得分:1)

回答问题的标题,因为其他人可能最终会像我一样在这里寻找一种从BigQuery时间戳创建YYYYMM年月的方法。

这是我在标准SQL中最终得到的代码:

CONCAT(CAST(EXTRACT(YEAR from timestamp) as string), LPAD(CAST(EXTRACT(MONTH from timestamp) as string),2,'0') ) as yearmonth

答案 3 :(得分:0)

我想你正在寻找这个

SELECT
  FORMAT_DATETIME('%B', DATETIME '2008-12-25 15:30:00')
  AS formatted;

因此DATETIME将您的字符串转换为日期时间格式,format_datetime()以获取月份名称

答案 4 :(得分:0)

我不理解你的评论"在bigquery中,我们使用像SQL" 这样的oracle,所以我只是假设你了在BigQuery中使用普通的标准SQL。

你的SQL有点奇怪,并没有多大意义,但这是一个公共数据集的工作示例。您需要更改时间戳列以匹配您的时间戳,并且我不知道total是什么,所以我只是做了1

SELECT
  SUM(CASE
      WHEN EXTRACT(month  FROM  community_owned_date ) = 1 AND EXTRACT(year  FROM  community_owned_date ) = 2017 THEN 1
      ELSE 0
    END ) AS January,
  SUM(CASE
      WHEN EXTRACT(month  FROM  community_owned_date ) = 2 AND EXTRACT(year  FROM  community_owned_date ) = 2017 THEN 1
      ELSE 0
    END ) AS February
FROM
  `bigquery-public-data.stackoverflow.posts_answers`