sql - 如何比较两个表,以便计算两个时间段之间的增长率?
问题描述
我目前拥有的表如下所示:(数据来自两个不同的表,19921231、19930331)
我要创建的表如下所示(添加了第 5 列)
目标:确定每家银行的存款增长率。即比较上一季度(例如 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
An answer to this question建议了这个代码,它可以工作但是,由于某种原因,我得到了“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
解决方案
您不应将此信息存储在不同的表中。这应该都在使用不同分区的同一张表中。但是由于日期嵌入在名称中间,我认为您需要使用显式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);
使用一张表中的数据会更简单。
推荐阅读
- javascript - 如何仅在自动完成材料 Ui 中显示搜索词?
- javascript - javascript中的私有字段是如何根据对象模型实现的?
- python - 读取 2 个 TXT 文件并解析文件
- javascript - 在Javascript / ReactNative中从数组中获取For of循环的值和索引的简单方法是什么?
- python-3.x - 许多条件应用于数据框列中的字符串,然后生成新列
- django - Django项目数据库
- c# - 颤动为什么我需要从另一个对象的位置中减去一个对象的位置来找到方向?
- python - 不能将数字排列成形状 (n,n) 矩阵
- java - 使用带有响应列表的 Retrofit/OkHttp 列出所有数据
- azure-databricks - Azure Databricks API:使用笔记本导入整个目录