【问题标题】:CASE WHEN on Google Data StudioGoogle Data Studio 上的案例
【发布时间】:2018-05-16 19:01:16
【问题描述】:

我试图在数据工作室上创建一个使用“case when”和函数 Date_diff 的字段。 我不断收到错误“无法解析公式”。

  1. 我是否需要指定两个日期之间差异的度量标准(就像我在 BigQuery 上所做的那样)?
  2. 我可以用数字而不是字符串来命名值吗(在 BigQuery 上尽可能这样做)?
  3. 我需要将所有字段转换为日期格式,还是 Data Studio 自己将它们识别为日期?
  4. 我可以使用“Between”功能吗?

我的最新版本(不起作用)如下:

case when date_diff(cast(checkin as date),order_date,day)>=0 and <=3 then 3

when date_diff(cast(checkin as date),order_date,day)>=4 and <=7 then 7

when date_diff(cast(checkin as date),order_date,day)>=8 and <=14 then 14

when date_diff(cast(checkin as date),order_date,day)>=15 and <=30 then 30

when date_diff(cast(checkin as date),order_date,day)>=31 and <=60 then 60

when date_diff(cast(checkin as date),order_date,day)>=61 and <=180 then 180

when date_diff(cast(checkin as date),order_date,day)>=181 and <=365 then 365

end

【问题讨论】:

  • sql-server mysql。您使用的是什么 RDBMS?
  • 你的case表达式不正确,应该是date_diff(cast(checkin as date),order_date,day)>=0 and date_diff(cast(checkin as date),order_date,day )
  • @Larnu 使用 BigQuery - 标准 SQL(它回答了吗?),很抱歉造成混淆。
  • @PSK 谢谢,刚刚注意到。你写的方法我试过了,还是不行。
  • @TomS 有时我会遇到这样的问题,即当我编辑已保存的字段时,我会不断收到解析错误。但是,如果我将代码复制并粘贴到一个新字段中,它会保存得很好。您可能想尝试一下,以消除您的代码有问题的事实。

标签: sql google-bigquery google-data-studio


【解决方案1】:

这是你的条件:

(case when date_diff(cast(checkin as date), order_date, day) >= 0 and <= 3 then 3
 . . .

这没有意义。您可以将其表示为

(case when date_diff(cast(checkin as date), order_date, day) between 0 and 3 then 3

我更倾向于这样写:

(case when checkin >= order_date and checkin < date_add(order_date, interval 4 day)

根据底层列的类型,您可能需要各种类型的转换才能完成这项工作。

【讨论】:

  • 谢谢!我最终定义了两个新字段——这是我能够创建我想要的东西的唯一方法。我创建了一个指定日期差异的文件。例如,一个字段由 date_diff(checkin,order_date) 定义,第二个字段使用第一个字段,而不是在案例中使用 date_diff 函数。然后它就起作用了。知道为什么它不允许我在案例中使用该功能吗?
猜你喜欢
  • 2018-04-20
  • 2022-06-21
  • 2016-08-23
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多