【问题标题】:Calculating the average of different investment rounds for different companies in PostgreSQL在 PostgreSQL 中计算不同公司不同投资轮次的平均值
【发布时间】:2021-01-13 10:14:44
【问题描述】:

我有一个表格,其中包含公司 ID、投资轮次名称(如 A、B、C 轮或 IPO ......),以及每个投资轮次的日期(如 2001-05-07)公司。 我想计算所有公司不同投资轮次的平均差距。例如,所有公司从 A 到 B 的平均时间是多少?所有公司从 B 到 C 的平均时间是多少?所有公司从 C 到 D 的平均时间是多少? 该表如下所示:

|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
---------------------------------------------------

感谢您的帮助!

【问题讨论】:

  • 请标记您正在使用的数据库。

标签: sql postgresql group-by average date-difference


【解决方案1】:

step-by-step demo:db<>fiddle

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_rounds 之间的持续时间。

现在您只需按the invest_rounds 分组并计算AVG 聚合。

【讨论】:

  • 太棒了。我的尝试太天真了!
  • @mhawke 。 . .我会非常谨慎地为此使用order by invest_round。我想你想要order by invest_date
  • @GordonLinoff 不,我不知道。投资日期与 TO 的问题无关。它仅用于计算持续时间。之后我想订购投资轮次,因为这是 TO 所要求的。在 B 和 C 之前订购invest_round A 似乎是正确的。
  • @S-Man:我认为@GordonLinoff 担心投资轮次名称可能并不总是按照您假设的方式排序。 OP 表示IPO 是一个有效的轮次名称。如果碰巧有一个名为J 的回合,如果存在这样的回合,它将在IPO 之后排序,这可能会影响您的查询。如果有一个名为SEED 的种子轮怎么办?按日期排序可以避免这个问题,因为这是一个公平的假设,即轮次按时间顺序而不是名称顺序发生。
【解决方案2】:

阶段 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 2020-05-20
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    相关资源
    最近更新 更多