如何比较两个表,以计算两个时间段之间的增长率?

Posted

技术标签:

【中文标题】如何比较两个表,以计算两个时间段之间的增长率?【英文标题】:How to compare two tables, in order to calculate the growth rate between two time periods? 【发布时间】:2020-09-12 04:14:53 【问题描述】:

我目前拥有的表格如下所示: (数据来自两个不同的表,19921231、19930331)

我要创建的表是这样的(添加了第 5 列)

目标:确定每家银行的存款增长率。 IE。比较上一季度在银行持有的存款(例如 19921231)与最近一个季度的存款(例如 19930331)。然后以百分比的形式查看增加/减少。

这是我目前写的代码:

select 
AL.repdte as `Date`, AL.cert, AL.name, AL.dep as `Deposits`
FROM usa_fdic_call_reports_1992.All_Reports_19921231_Assets_and_Liabilities as AL

UNION ALL

select 
AL.repdte as `Date`, AL.cert, AL.name, AL.dep as `Deposits`
FROM usa_fdic_call_reports_1993.All_Reports_19930331_Assets_and_Liabilities as AL


对这个问题的回答建议了这个代码,它有效 但是,由于某种原因,我得到了“NULL”的输出

select al19930331.repdte as `Date`, al19930331.cert, al19930331.name,
       al19930331.dep as Deposits_1993,
       al19921231.dep as Deposits_1992,
       (al19930331.dep - al19921231.dep) / al19921231.dep as grow_rate
from usa_fdic_call_reports_1993.All_Reports_19930331_Assets_and_Liabilities as al19930331 left join
     usa_fdic_call_reports_1992.All_Reports_19921231_Assets_and_Liabilities as al19921231
     on al19930331.cert = al19921231.cert and
        al19930331.name = al19921231.name and
        al19921231.repdte = date_add(al19930331.repdte, interval 1 year);

为了隔离“NULL”问题,我将查询简化为最简单的术语,并且能够消除“NULL”问题。

现在我们有两个季度的存款列,返回似乎是正确的输出。

接下来我删除了最后一行代码:

select al19930331.repdte as `Date`, al19930331.cert, al19930331.name,
       al19930331.dep as Deposits_1993,
       al19921231.dep as Deposits_1992,
       (al19930331.dep - al19921231.dep) / al19921231.dep as grow_rate
from usa_fdic_call_reports_1993.All_Reports_19930331_Assets_and_Liabilities as al19930331 left join
     usa_fdic_call_reports_1992.All_Reports_19921231_Assets_and_Liabilities as al19921231
     on al19930331.cert = al19921231.cert and
        al19930331.name = al19921231.name and
        al19921231.repdte = date_add(al19930331.repdte, interval 1 year);

删除最后一行代码确实有效。运行代码会产生“除以零”错误。如何消除除以零误差?

select al19930331.repdte as `Date`, al19930331.cert, al19930331.name,
       al19930331.dep as Deposits_1993,
       al19921231.dep as Deposits_1992,
       (al19930331.dep - al19921231.dep) / al19921231.dep as grow_rate
from usa_fdic_call_reports_1993.All_Reports_19930331_Assets_and_Liabilities as al19930331 left join
     usa_fdic_call_reports_1992.All_Reports_19921231_Assets_and_Liabilities as al19921231
     on al19930331.cert = al19921231.cert and
        al19930331.name = al19921231.name

【问题讨论】:

你的数据库是什么?如果是 mysql,您使用的是版本 8 还是更高版本? 使用 BigQuery 标准 SQL 您的回复中有一个真实的LAG,但请参阅@GMB 下面的有用答案,看起来是正确的。 【参考方案1】:

您不应将此信息存储在不同的表中。这应该都在同一个表中,使用不同的分区。但是由于名称中间嵌入了日期,我认为您需要使用明确的join

    select al19930331.repdte as `Date`, al19930331.cert, al19930331.name,
       al19930331.dep as Deposits_1993_0331,
       al19921231.dep as Deposits_1992_1231,
       (al19930331.dep - al19921231.dep) / al19921231.dep as grow_rate
from usa_fdic_call_reports_1993.All_Reports_19930331_Assets_and_Liabilities as al19930331 left join
     usa_fdic_call_reports_1992.All_Reports_19921231_Assets_and_Liabilities as al19921231
     on al19930331.cert = al19921231.cert and 
     al19921231.repdte = date_add(al19930331.repdte, interval 1 quarter);

将数据放在一个表中会更简单。

【讨论】:

在 Deposits_1992 和 Growth_rate 上接收 NULL。选择al19930331.repdte如Date,al19930331.cert,al19930331.name,al19930331.dep如Deposits_1993_0331,al19921231.dep如Deposits_1992_1231,(al19930331.dep - al19921231.dep)/ al19921231.dep从usa_fdic_call_reports_1993.All_Reports_19930331_Assets_and_Liabilities grow_rate作为al19930331 left join usa_fdic_call_reports_1992.All_Reports_19921231_Assets_and_Liabilities as al19921231 on al19930331.cert = al19921231.cert 和 al19921231.repdte = date_add(al19930331.repdte, 间隔 1 季度); @user2755660 。 . .那么您在上一年没有匹配的值。也许名称不匹配。 将查询配对以消除 Null。现在正在返回两个季度的存款值。现在只需要推理。想知道这是否可能是除法/零问题。选择al19930331.repdte为Date,al19930331.cert,al19930331.name,al19930331.dep为Deposits_1993_0331,al19921231.dep为Deposits_1992_1231,从usa_fdic_call_reports_1993.All_Reports_19930331_Assets_and_Liabilities作为al19930331左连接usa_fdic_call_reports_1992.All_Reports_19921231_Assets_and_Liabilities作为al19921231上al19930331.cert = al19921231.cert 【参考方案2】:

你可以使用lag():

select
    t.*,
    100.0 
        * (deposits - lag(deposits) over(partition by cert order by depdte)) 
        / lag(deposits) over(partition by cert order by depdte) as growth_percent
from mytable t

【讨论】:

以上是关于如何比较两个表,以计算两个时间段之间的增长率?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Oracle 中对两个远程表之间的数据进行部分比较以避免 ORA-01652

从两个不同的表计算增长百分比时除以零误差

如何在php中计算两个位置之间的距离(以公里为单位)?

如何使用mysql计算两个日期之间的时间差

SQL Server - 以 HH:MM:SS 格式计算两个日期时间戳之间的经过时间

比较与特定字段有关的两个表之间的差异时如何正确连接