在PostgreSQL中计算不同公司不同投资轮次的平均值
Calculating the average of different investment rounds for different companies in PostgreSQL
我有一个 table 包含 company_id、投资轮次名称(如 A、B、C 轮或 IPO ......)和日期(如 2001- 05-07) 每家公司的每轮投资。
我想计算所有公司不同投资轮次的平均差距。例如,所有公司从 A 到 B 的平均时间是多少?所有公司从 B 到 C 的平均时间是多少?所有公司从 C 到 D 的平均时间是多少?
table 看起来像这样:
|company_id| |invest_rounds_type_name| |invest_date|
---------------------------------------------------
1 A 2001-01-01
---------------------------------------------------
1 B 2001-12-05
---------------------------------------------------
1 C 2003-11-12
---------------------------------------------------
2 A 1963-03-01
---------------------------------------------------
2 B 1967-10-10
---------------------------------------------------
2 C 1970-10-12
---------------------------------------------------
2 D 1971-01-05
---------------------------------------------------
3 B 2017-11-20
---------------------------------------------------
3 A 2017-11-16
---------------------------------------------------
3 C 2018-03-19
---------------------------------------------------
感谢您的帮助!
阶段 A 和 B 之间的示例:
-- table is named 'x'
select avg(diff) from (
select xb.invest_date - xa.invest_date as diff
from x xa join x xb on (xa.company_id = xb.company_id)
where xa.invest_rounds_type_name = 'A' and
xb.invest_rounds_type_name = 'B'
) as gaps;
当 运行 对您的数据产生(天):
平均
675.3333333333334
sqlfiddle: http://sqlfiddle.com/#!17/3559c/23
SELECT
invest_round as invest_round_start,
invest_round_end,
AVG(days_required)
FROM (
SELECT
*,
lead(invest_round) OVER w as invest_round_end,
lead(invest_date) OVER w - invest_date as days_required
FROM mytable
WINDOW w AS (PARTITION BY company_id ORDER BY invest_round)
) s
WHERE invest_round_end IS NOT NULL
GROUP BY invest_round, invest_round_end
ORDER BY invest_round
通过使用 lead()
window function,您可以将特定列的下一个值复制到当前列。这样就可以得到下面的invest_round
到当前的记录以及下面的invest_date
.
使用以下日期和当前日期,您可以计算出两个 invest_round
之间的持续时间。
现在您只需按 the invest_round
分组并计算 AVG
总和。
我有一个 table 包含 company_id、投资轮次名称(如 A、B、C 轮或 IPO ......)和日期(如 2001- 05-07) 每家公司的每轮投资。 我想计算所有公司不同投资轮次的平均差距。例如,所有公司从 A 到 B 的平均时间是多少?所有公司从 B 到 C 的平均时间是多少?所有公司从 C 到 D 的平均时间是多少? table 看起来像这样:
|company_id| |invest_rounds_type_name| |invest_date|
---------------------------------------------------
1 A 2001-01-01
---------------------------------------------------
1 B 2001-12-05
---------------------------------------------------
1 C 2003-11-12
---------------------------------------------------
2 A 1963-03-01
---------------------------------------------------
2 B 1967-10-10
---------------------------------------------------
2 C 1970-10-12
---------------------------------------------------
2 D 1971-01-05
---------------------------------------------------
3 B 2017-11-20
---------------------------------------------------
3 A 2017-11-16
---------------------------------------------------
3 C 2018-03-19
---------------------------------------------------
感谢您的帮助!
阶段 A 和 B 之间的示例:
-- table is named 'x'
select avg(diff) from (
select xb.invest_date - xa.invest_date as diff
from x xa join x xb on (xa.company_id = xb.company_id)
where xa.invest_rounds_type_name = 'A' and
xb.invest_rounds_type_name = 'B'
) as gaps;
当 运行 对您的数据产生(天):
平均 675.3333333333334
sqlfiddle: http://sqlfiddle.com/#!17/3559c/23
SELECT
invest_round as invest_round_start,
invest_round_end,
AVG(days_required)
FROM (
SELECT
*,
lead(invest_round) OVER w as invest_round_end,
lead(invest_date) OVER w - invest_date as days_required
FROM mytable
WINDOW w AS (PARTITION BY company_id ORDER BY invest_round)
) s
WHERE invest_round_end IS NOT NULL
GROUP BY invest_round, invest_round_end
ORDER BY invest_round
通过使用 lead()
window function,您可以将特定列的下一个值复制到当前列。这样就可以得到下面的invest_round
到当前的记录以及下面的invest_date
.
使用以下日期和当前日期,您可以计算出两个 invest_round
之间的持续时间。
现在您只需按 the invest_round
分组并计算 AVG
总和。