带小数值的天数的日期差异,不包括周末

时间:2016-06-22 23:25:10

标签: sql google-bigquery

我想获得两个时间戳之间的确切天数(例如1.45天)。事情是BigQuery datediff函数在当天完成,只接受2个时间戳参数。

SELECT
datediff(start_time_pac_tz, end_time_pac_tz) as Date_difference
  

Date_difference

     

-6

我也想排除周末。任何帮助是极大的赞赏。

1 个答案:

答案 0 :(得分:1)

使用TIMESTAMP_TO_USEC(<timestamp>),需要一天=一秒* 60 * 60 * 24.

SELECT
    ((TIMESTAMP_TO_USEC(end_time_pac_tz) - TIMESTAMP_TO_USEC(start_time_pac_tz)) / (60 * 60 * 24) + 1)
  -((WEEK(end_time_pac_tz) - WEEK(start_time_pac_tz)) * 2)
  -(CASE WHEN DAYOFWEEK(start_time_pac_tz) = 1 THEN 1 ELSE 0 END)
  -(CASE WHEN DAYOFWEEK(end_time_pac_tz) = 7 THEN 1 ELSE 0 END)
  as Date_difference