如何对计算列的结果求和
how to sum results of a calculated column
我正在尝试获取会员类型(会员和临时会员)的总持续时间同一问题的另一个应用是获取人口总数和特定组的总数,例如成员然后使用结果来计算成员代表的人口百分比
member_casual
started_at,
ended_at,
ended_at-started_at as duration
from `cyclistic-case-study-349400.2020_q1.4`
group by member_casual = 'member'```
also tried
```select
member_casual
started_at,
ended_at,
sum(ended_at-started_at) as duration
from `cyclistic-case-study-349400.2020_q1.4`
where member_casual = 'member'```
any help will be appreciated
您需要使用 date function or datetime functions。
根据您需要的信息,您必须选择日月,您需要哪种输出格式
select
member_casual
started_at,
ended_at,
sum(DATE_DIFF(ended_at,started_at, DAY)) as duration
from `cyclistic-case-study-349400.2020_q1.4`
where member_casual = 'member'
或
select
member_casual
started_at,
ended_at,
sum(DATETIME_DIFF(ended_at,started_at, DAY)) as duration
from `cyclistic-case-study-349400.2020_q1.4`
where member_casual = 'member'
从下面的查询中,您可以得到每个 member_casual
.
的持续时间
SELECT member_casual,
SUM(DATE_DIFF(ended_at, started_at, DAY)) AS duration
FROM `cyclistic-case-study-349400.2020_q1.4`
GROUP BY 1
对于您提到的其他指标,您需要使用window(分析)函数或需要子查询来获取总人口来计算百分比。
(更新)
我已经用测试数据集进行了尝试并得到了以下输出。
CREATE OR REPLACE EXTERNAL TABLE `my_dataset.cyclistic_case_study_349400`
OPTIONS (
format = 'CSV',
skip_leading_rows = 1,
field_delimiter = ',',
uris = ['https://drive.google.com/open?id=1q8d2nwUTGH33MRpWShU-2dMhjYC1JANJ']
);
SELECT member_casual,
SUM(DATE_DIFF(ended_at, started_at, DAY)) AS duration
FROM `my_dataset.cyclistic_case_study_349400`
GROUP BY 1;
+---------------+----------+
| member_casual | duration |
+---------------+----------+
| member | 137 |
| casual | 509 |
+---------------+----------+
我正在尝试获取会员类型(会员和临时会员)的总持续时间同一问题的另一个应用是获取人口总数和特定组的总数,例如成员然后使用结果来计算成员代表的人口百分比
member_casual
started_at,
ended_at,
ended_at-started_at as duration
from `cyclistic-case-study-349400.2020_q1.4`
group by member_casual = 'member'```
also tried
```select
member_casual
started_at,
ended_at,
sum(ended_at-started_at) as duration
from `cyclistic-case-study-349400.2020_q1.4`
where member_casual = 'member'```
any help will be appreciated
您需要使用 date function or datetime functions。
根据您需要的信息,您必须选择日月,您需要哪种输出格式
select
member_casual
started_at,
ended_at,
sum(DATE_DIFF(ended_at,started_at, DAY)) as duration
from `cyclistic-case-study-349400.2020_q1.4`
where member_casual = 'member'
或
select
member_casual
started_at,
ended_at,
sum(DATETIME_DIFF(ended_at,started_at, DAY)) as duration
from `cyclistic-case-study-349400.2020_q1.4`
where member_casual = 'member'
从下面的查询中,您可以得到每个 member_casual
.
SELECT member_casual,
SUM(DATE_DIFF(ended_at, started_at, DAY)) AS duration
FROM `cyclistic-case-study-349400.2020_q1.4`
GROUP BY 1
对于您提到的其他指标,您需要使用window(分析)函数或需要子查询来获取总人口来计算百分比。
(更新) 我已经用测试数据集进行了尝试并得到了以下输出。
CREATE OR REPLACE EXTERNAL TABLE `my_dataset.cyclistic_case_study_349400`
OPTIONS (
format = 'CSV',
skip_leading_rows = 1,
field_delimiter = ',',
uris = ['https://drive.google.com/open?id=1q8d2nwUTGH33MRpWShU-2dMhjYC1JANJ']
);
SELECT member_casual,
SUM(DATE_DIFF(ended_at, started_at, DAY)) AS duration
FROM `my_dataset.cyclistic_case_study_349400`
GROUP BY 1;
+---------------+----------+
| member_casual | duration |
+---------------+----------+
| member | 137 |
| casual | 509 |
+---------------+----------+