使用 SQL 过滤数据

问题描述 投票:0回答:1

在R studio中,我们可以从A列中减去B列的值,存入一个新的C列。然后下一步我们就可以计算了,求和,求平均值,在上面进行各种计算。 我怎样才能在大查询中用 SQL 做同样的事情?它不允许我在同一页上写两个选择语句。在清理或过滤某些东西后,如何保存我的数据以执行更多计算?

提前谢谢你

我从 B 列中减去 A 列的值,并将它们放在新的“持续时间”列中。然后我卡住了。我试图仅对客户类型 X 的持续时间求和。

SELECT (ended_at - started_at) AS duration, customer_type 来自

my-project-81752.loyal_customer.jan22

我的结果是;

Row duration         customer_type
1   0-0 0 0:15:5      X
2   0-0 0 0:19:1      X
3   0-0 0 0:11:34     Y
4   0-0 0 0:14:26     X
5   0-0 0 0:3:3       Y
6   
...

现在我如何总结持续时间以获得客户 X 的总持续时间?

我需要在使用 SUM 之前将结果存储在某个地方吗?

bigquery 与其他 sql 数据库有什么不同吗?

sql google-bigquery data-cleaning qsqlquery data-filtering
1个回答
2
投票

如果你想要 customer_type "X" 的总持续时间,那么 group by with aggregation 应该可以解决问题。请试试这个:

SELECT sum(ended_at - started_at) AS total_duration, customer_type FROM my-project-81752.loyal_customer.jan22 where customer_type='X'
group by customer_type
© www.soinside.com 2019 - 2024. All rights reserved.