Bigquery中的两位数ISO周格式(ww)

时间:2017-02-28 13:15:35

标签: google-bigquery

我想在Bigquery中使用两位数的ISO周格式(ww)

"select week(date)..." //statement results in a one digit week format.

感谢您的提前帮助

3 个答案:

答案 0 :(得分:2)

使用standard SQL,您可以将a功能与FORMAT_DATE一起使用,以获取日期的ISO周号。例如,

%V

答案 1 :(得分:1)

取决于您的业务逻辑 - 除了%W

之外,您可能还需要考虑%V

请尝试以下方法查看差异

#standardSQL
WITH SampleDates AS (
  SELECT DATE '2017-01-01' AS day UNION ALL
  SELECT DATE '2017-02-28' UNION ALL
  SELECT DATE '2016-12-15' 
)
SELECT
  day,
  FORMAT_DATE('%a', day) AS weekday,
  FORMAT_DATE('%V', day) AS weeknumber1,
  FORMAT_DATE('%W', day) AS weeknumber2
FROM SampleDates
ORDER BY day

day         weekday weeknumber1 weeknumber2  
2016-12-15  Thu     50          50   
2017-01-01  Sun     52          00   
2017-02-28  Tue     09          09    

查看2017-01-01的差异 这是因为

%V  The week number of the year (Monday as the first day of the week) 
    as a decimal number (01-53).   
    If the week containing January 1 has four or more days in the new year,  
    then it is week 1; otherwise it is week 53 of the previous year,   
    and the next week is week 1.  

%W  The week number of the year (Monday as the first day of the week)   
    as a decimal number (00-53).

同时,您仍然可以在旧版SQL中使用两位数字。 Ty下面

#legacySQL
SELECT 
  day,
  RIGHT(STRING(100 + WEEK(day)), 2) as weeknumber
FROM (SELECT '2017-01-01' AS day),
     (SELECT '2017-02-28' AS day),
     (SELECT '2016-12-15' AS day)
ORDER BY day     

答案 2 :(得分:0)

请注意,如果使用%V(01-53),第52周或第53周可能会持续数年。 第52/53周可能属于前一年(如果日期是某些年份的1月1日至3日,例如2017年)或当前年份(如果日期是12月底)。

要指定相应的年份,您可以使用%G(4位数)或%g(2位数)。

另一方面,第1周也可能是暧昧的,属于明年(如果日期是某些年份的12月底,例如2018年)或当前年份(如果日期是1月)。

Row date        ...     ...         iso_week    W_G 
 12 2019-01-07  ...     ...         02 - 2019   01 - 2019

以%W取代%V可以避免歧义但是您会丢失ISO编号 - 请参阅2019-01-07:

line = "\\\"time\\\":1505050505,\\\"event\\\":check";
line = line.replaceAll("\\\"", "\"");
相关问题