Более простой способ изменить имя таблицы с Q1 на Q2 (нужно изменить более 100 имен таблиц) - PullRequest
0 голосов
/ 10 февраля 2019

Есть ли способ изменить часть имени таблицы?

1002 * Начните с: 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

Вывод: enter image description here

Однако мне нужно запросить более 100 финансовых кварталов / таблиц за 20 с лишним лет.Поэтому мне нужно использовать UNION ALL и изменить 5 цифр в каждом [FROM] имени таблицы.

Вот пример вышеупомянутого блока кода, повторно используемого для запроса нескольких финансовых кварталов (т. Е. Запроса нескольких таблиц):

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

Вывод: для финансовых 5 кварталов (5 таблиц).enter image description here

Изменение копии / вставки каждого блока кода займет много времени, а затем даты в названии каждой таблицы (например, 20170630 на 20170331 ).И сделать это более 100 раз для каждой таблицы.

Какие есть решения для облегчения этого процесса?Или мне просто нужно скопировать / вставить каждый блок кода и изменить финансовый квартал в трех местах.Затем повторите этот процесс более 100 раз.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...