简体   繁体   English

在 Google Data Studio 或 SQL 中避免数据聚合中的“无穷大”

[英]Avoid 'infinity' in data aggregates in Google Data Studio or SQL

I am building a report in Data Studio, and the data comes from a BigQuery dataset.我正在 Data Studio 中构建报告,数据来自 BigQuery 数据集。

The problem is that some values in the table are Infinity , and when Data Studio aggregates in totals or average, the result is always Infinity.问题是表中的某些值是Infinity ,当 Data Studio 聚合总计或平均值时,结果始终是 Infinity。

I need to avoid those values in those aggregates.我需要避免这些聚合中的那些值。 I am trying to do that in Data Studio, or to replace those values by zero in the SQL query.我正在尝试在 Data Studio 中执行此操作,或者在 SQL 查询中将这些值替换为零。

Is there a way to do either of those things?有没有办法做这两件事? Is there another way to deal with this?还有另一种方法可以解决这个问题吗?

Thanks in advance.提前致谢。

Using a Case Statement in your aggregation is likely where you'd like to go.在您的聚合中使用 Case Statement 很可能是您想要去的地方。 If you want to replace this with 0 then the below logic will work.如果你想用 0 替换它,那么下面的逻辑将起作用。

SELECT
   SUM(
      CASE
         WHEN col2 = inf THEN 0
         ELSE col2
      END
    ) as aggregated_value
FROM TABLE 1;

If you need to do an average, then 0 isn't a good replacement.如果你需要做一个平均值,那么 0 不是一个好的替代品。 Instead use NULL as SQL will ignore NULLS in an average:而是使用 NULL,因为 SQL 将平均忽略 NULLS:

SELECT
   AVG(
      CASE
         WHEN col2 = inf THEN NULL
         ELSE col2
      END
    ) as aggregated_value
FROM TABLE 1;

You can use the "is_inf" function in the where clause and remove any bad data records before aggregation您可以在 where 子句中使用 "is_inf" function 并在聚合前删除任何不良数据记录

SELECT
   SUM(col2) as aggregated_value
FROM TABLE 1
WHERE IS_INF(col2) = false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM