是否可以更改表名的一部分?
开始于:All_Reports_19920331_Performance_and_Condition_Ratios 改成: All_Reports_19920630_Performance_and_Condition_Ratios 改成: All_Reports_19920930_Performance_and_Condition_Ratios 改成: All_Reports_19921231_Performance_and_Condition_Ratios 改成: All_Reports_19930331_Performance_and_Condition_Ratios 改成: All_Reports_19930630_Performance_and_Condition_Ratios 改成: All_Reports_19930930_Performance_and_Condition_Ratios 更改为:All_Reports_19931231_Performance_and_Condition_Ratios
从1992年到2018年重复
我有一个来自政府监管机构FDIC的公开数据集,其中包含每个银行的信息。
此代码块仅查询一个财务季度。
SELECT
(SELECT
replace(substr(PCR.repdte,6),'/','')||'q'||CAST(1+ .
((substr(PCR.repdte,1,2)-1) / 3) AS INTEGER) --converts
date format from dd/mm/yyyy to yyyyq1 (financial
quarters)
FROM
All_Reports_20170630_Performance_and_Condition_Ratios
as PCR)
AS Quarter,
(SELECT
AVG(PCR.lnlsdepr)
FROM
All_Reports_20170630_Performance_and_Condition_Ratios
as PCR)
AS NetLoansAndLeasesToDeposits,
(SELECT sum(CAST(LD.IDdepsam as int))
FROM
'All_Reports_20170630_Deposits_Based_on_the
_$250,000_Reporting_Threshold' AS LD)
AS DepositAccountsWith$LessThan$250k
但是我需要查询20多年以来的100多个财务季度/表格。因此,我需要使用UNION ALL,并在每个[FROM]表名中更改5位数字。
以下是上述代码块被重用于查询多个财务季度(即查询多个表)的示例:
SELECT
(SELECT
replace(substr(PCR.repdte,6),'/','')||'q'||CAST(1+ .
((substr(PCR.repdte,1,2)-1) / 3) AS INTEGER) --converts date format from
dd/mm/yyyy to yyyyq1 (financial quarters)
FROM All_Reports_20170630_Performance_and_Condition_Ratios as PCR)
AS Quarter,
(SELECT
AVG(PCR.lnlsdepr)
FROM All_Reports_20170630_Performance_and_Condition_Ratios as PCR)
AS NetLoansAndLeasesToDeposits,
(SELECT sum(CAST(LD.IDdepsam as int))
FROM
'All_Reports_20170630_Deposits_Based_on_the_$250,000_Reporting_Threshold'
AS LD)
AS DepositAccountsWith$LessThan$250k
UNION ALL
SELECT
(SELECT
replace(substr(PCR.repdte,6),'/','')||'q'||CAST(1+ .
((substr(PCR.repdte,1,2)-1) / 3) AS INTEGER) --converts date format from
dd/mm/yyyy to yyyyq1 (financial quarters)
FROM All_Reports_20170930_Performance_and_Condition_Ratios as PCR)
AS Quarter,
(SELECT
AVG(PCR.lnlsdepr)
FROM All_Reports_20170930_Performance_and_Condition_Ratios as PCR)
AS NetLoansAndLeasesToDeposits,
(SELECT sum(CAST(LD.IDdepsam as int))
FROM
'All_Reports_20170930_Deposits_Based_on_the_$250,000_Reporting_Threshold'
AS LD)
AS DepositAccountsWith$LessThan$250k
UNION ALL
SELECT
(SELECT
replace(substr(PCR.repdte,6),'/','')||'q'||CAST(1+ .
((substr(PCR.repdte,1,2)-1) / 3) AS INTEGER) --converts date format
from dd/mm/yyyy to yyyyq1 (financial quarters)
FROM All_Reports_20171231_Performance_and_Condition_Ratios as PCR)
AS Quarter,
(SELECT
AVG(PCR.lnlsdepr)
FROM All_Reports_20171231_Performance_and_Condition_Ratios as PCR)
AS NetLoansAndLeasesToDeposits,
(SELECT sum(CAST(LD.IDdepsam as int))
FROM
'All_Reports_20171231_Deposits_Based_on_the_$250,000_Reporting_Threshold'
AS LD)
AS DepositAccountsWith$LessThan$250k
UNION ALL
SELECT
(SELECT
replace(substr(PCR.repdte,6),'/','')||'q'||CAST(1+ .
((substr(PCR.repdte,1,2)-1) / 3) AS INTEGER) --converts date format
from dd/mm/yyyy to yyyyq1 (financial quarters)
FROM All_Reports_20180331_Performance_and_Condition_Ratios as PCR)
AS Quarter,
(SELECT
AVG(PCR.lnlsdepr)
FROM All_Reports_20180331_Performance_and_Condition_Ratios as PCR)
AS NetLoansAndLeasesToDeposits,
(SELECT sum(CAST(LD.IDdepsam as int))
FROM
'All_Reports_20180331_Deposits_Based_on_the
_$250,000_Reporting_Threshold' AS LD)
AS DepositAccountsWith$LessThan$250k
UNION ALL
SELECT
(SELECT
replace(substr(PCR.repdte,6),'/','')||'q'||CAST(1+ .
((substr(PCR.repdte,1,2)-1) / 3) AS INTEGER) --converts date format
from dd/mm/yyyy to yyyyq1 (financial quarters)
FROM All_Reports_20180630_Performance_and_Condition_Ratios as PCR)
AS Quarter,
(SELECT
AVG(PCR.lnlsdepr)
FROM All_Reports_20180630_Performance_and_Condition_Ratios as PCR)
AS NetLoansAndLeasesToDeposits,
(SELECT sum(CAST(LD.IDdepsam as int))
FROM
'All_Reports_20180630_Deposits_Based_on_the_$250,000_Reporting_Threshold' AS
LD) AS DepositAccountsWith$LessThan$250k
UNION ALL
SELECT
(SELECT
replace(substr(PCR.repdte,6),'/','')||'q'||CAST(1+ .
((substr(PCR.repdte,1,2)-1) / 3) AS INTEGER) --converts date format from
dd/mm/yyyy to yyyyq1 (financial quarters)
FROM All_Reports_20170930_Performance_and_Condition_Ratios as PCR)
AS Quarter,
(SELECT
AVG(PCR.lnlsdepr)
FROM All_Reports_20170930_Performance_and_Condition_Ratios as PCR)
AS NetLoansAndLeasesToDeposits,
(SELECT sum(CAST(LD.IDdepsam as int))
FROM
'All_Reports_20170930_Deposits_Based_on_the_$250,000_Reporting_Threshold'
AS LD)
AS DepositAccountsWith$LessThan$250k
更改复制/粘贴每个代码块,然后更改每个表名中的日期(例如 20170630 到 20170331 )将非常耗时。并为每个表执行100多次。
有哪些解决方案可简化此过程?还是只需要复制/粘贴每个代码块并在三个位置更改财务季度。然后重复此过程100多次。