如何将开始日期参数转换为微秒在自定义查询中?

huangapple go评论66阅读模式
英文:

how to convert start date parameter to microsecond in custom query?

问题

I try to use custom query in Google Looker Studio as the data source. and here is the custom SQL to fetch data from my BigQuery

WITH analytics_data AS (
  SELECT user_pseudo_id, 
         UNIX_MICROS(TIMESTAMP("2023-03-10 00:00:00", "+7:00")) AS start_day,
         3600*1000*1000*24*7 AS one_week_micros
  FROM `project.analytics_297774888.events_*`
  WHERE _table_suffix BETWEEN @DS_START_DATE AND @DS_END_DATE
)

as you can see I enable date range parameters, so I use @DS_START_DATE and @DS_END_DATE . my problem is in this one line

UNIX_MICROS(TIMESTAMP("2023-03-10 00:00:00", "+7:00")) AS start_day

currently it is hardcoded string ("2023-03-10 00:00:00"). I want to replace the date string with @DS_START_DATE. If I change the code to be like one below, I will have error

UNIX_MICROS(TIMESTAMP(@DS_START_DATE, "+7:00")) AS start_day

I am trying to get Unix Microsecond (number of microseconds since 1970-01-01) of date @DS_START_DATE at 00:00:00 in timezone UTC + 7

how to do that?

英文:

I try to use custom query in Google Looker Studio as the data source. and here is the custom SQL to fetch data from my BigQuery

WITH analytics_data AS (
  SELECT user_pseudo_id, 
         UNIX_MICROS(TIMESTAMP("2023-03-10 00:00:00", "+7:00")) AS start_day,
         3600*1000*1000*24*7 AS one_week_micros
  FROM `project.analytics_297774888.events_*`
  WHERE _table_suffix BETWEEN @DS_START_DATE AND @DS_END_DATE
)

as you can see I enable date range parameters, so I use @DS_START_DATE and @DS_END_DATE . my problem is in this one line

UNIX_MICROS(TIMESTAMP("2023-03-10 00:00:00", "+7:00")) AS start_day

currently it is hardcoded string ("2023-03-10 00:00:00"). I want to replace the date string with @DS_START_DATE. If I change the code to be like one below, I will have error

UNIX_MICROS(TIMESTAMP(@DS_START_DATE, "+7:00")) AS start_day

I am trying to get Unix Microsecond (number of microseconds since 1970-01-01) of date @DS_START_DATE at 00:00:00 in timezone UTC + 7

how to do that?

答案1

得分: 1

@DS_START_DATE@DS_END_DATE 的格式为 YYYYmmdd,您可以使用 PARSE_TIMESTAMP 来解析它,如下所示,同时指定时区。

UNIX_MICROS(PARSE_TIMESTAMP('%Y%m%d', @DS_START_DATE, '+7:00'))
英文:

Since @DS_START_DATE and @DS_END_DATE has a shape of YYYYmmdd, you can parse it using PARSE_TIMESTAMP like below along with timezone.

UNIX_MICROS(PARSE_TIMESTAMP('%Y%m%d', @DS_START_DATE, '+7:00'))

答案2

得分: 0

以下是翻译好的代码部分:

尝试这个:

     WITH analytics_data AS (
      SELECT user_pseudo_id, 
             UNIX_MICROS(TIMESTAMP(CONCAT(@DS_START_DATE, " 00:00:00"), "+7:00") AS start_day,
             3600*1000*1000*24*7 AS one_week_micros
      FROM `project.analytics_297774888.events_*`
      WHERE _table_suffix BETWEEN @DS_START_DATE AND @DS_END_DATE
    )
英文:

Try this:

 WITH analytics_data AS (
  SELECT user_pseudo_id, 
         UNIX_MICROS(TIMESTAMP(CONCAT(@DS_START_DATE, " 00:00:00"), "+7:00")) AS start_day,
         3600*1000*1000*24*7 AS one_week_micros
  FROM `project.analytics_297774888.events_*`
  WHERE _table_suffix BETWEEN @DS_START_DATE AND @DS_END_DATE
)

huangapple
  • 本文由 发表于 2023年3月23日 11:36:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定