【问题标题】:I'm trying to count how many transfers took more than 30 minutes, and this error keeps showing up我正在尝试计算有多少传输时间超过 30 分钟,并且此错误不断出现
【发布时间】:2020-02-04 09:05:13
【问题描述】:

这是我写的公式↓

count(if(avg(timestamp_diff(broker_delivery_date,request_datetime,minute)),0) >= 30,id,NULL)

这是不断出现的错误↓

对于参数类型的函数 IF 没有匹配的签名:FLOAT64, INT64。支持的签名:IF(BOOL, ANY, ANY) at [10:10]

【问题讨论】:

  • 阅读括号
  • 我不太了解 bigquery,但像任何 sql 一样,您可以使用 where 子句,并选择所需的列。在你的情况下 count(*) 也应该工作。
  • @PaganottiFelipeSato 。 . .您的代码看起来像是嵌套聚合函数count()avg()。这在任何数据库中通常是不允许的。示例数据、所需结果以及对您要实现的逻辑的解释都会有所帮助。

标签: sql google-bigquery


【解决方案1】:

这个结构有几个问题。

首先,,0) 是什么。这没有意义。也许你打算:

count(if(avg(timestamp_diff(broker_delivery_date, request_datetime, minute
                           )
            ) >= 30,
         id,   -- then
         NULL  -- else
     )

更重要的是,您正在嵌套聚合函数,这是不允许的。一种可能性是您根本不打算使用avg(),而只想查看单行中的值。这也可以简化,使用countif():

countif(timestamp_diff(broker_delivery_date, request_datetime, minute
                      ) >= 30
       )

我猜这就是你真正想要的。

【讨论】:

  • 戈登·林诺夫爵士感谢您的回答。它奏效了。
  • @PaganottiFelipeSato 。 . .如果您的问题得到解答,我建议您接受这个(或另一个)答案。
【解决方案2】:

假设您的问题是“为什么会这样?”:

IF(BOOL, ANY, ANY)

需要三个参数。你只给它两个。这是它的处理方式:

count(if(
         avg(timestamp_diff(broker_delivery_date,request_datetime,minute)),  <-- This is not a BOOL
         0  <-- This is the second parameter to if
         ) >= 30,  <-- This is a BOOL as the first parameter to count
      id,  <-- This is a second parameter to count
      NULL) <-- This is the third parameter to count

试试这个(免责声明:未经测试):

count(if(avg(timestamp_diff(broker_delivery_date,request_datetime,minute)) >= 30,
         1,
         NULL)
      )

【讨论】:

    【解决方案3】:

    IF 语句需要 boolean 表达式。如下所示,您在错误的位置关闭了括号,这导致 IF 参数是 FLOAT(AVG 结果)而不是表达式 @987654324 @如你所料。

    count(
        if(
           avg(
               timestamp_diff(broker_delivery_date,request_datetime,minute)
              )
          ,0
          ) >= 30,id,NULL)
    

    你可能想要做的是:

    count(
        if(
          avg(timestamp_diff(broker_delivery_date,request_datetime,minute)) >= 30
          ,id,NULL
          )
     )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多