0

長い SQL スクリプトがあります。非常に醜いのは、既存のコードを何度もコピーして貼り付けることを余儀なくされたことです。既存のコードを参照することをお勧めします。

私の目標は、すべてのコード ブロックを 1 回だけ記述することです。もう一度必要な場合は、それを参照します。これはビューなしで可能ですか?

SELECT batch
FROM (SELECT    'DISCONNECT ent_user  FROM job_code WITH user_id ="'
             || a.user_id
             || '" , jc_name = "'
             || b.jc_name
             || '";'
                AS batch,
             1 AS my_order
        FROM ent_user a, user_jc b
       WHERE a.user_id IN (SELECT user_id
                           FROM (SELECT REPLACE (overview.entity_key,
                                                 'user_id=',
                                                 '')
                                           AS USER_ID,
                                        details.old_value_str,
                                        SUBSTR (
                                           details.old_value_str,
                                           1,
                                           INSTR (details.old_value_str,
                                                  '-',
                                                  1,
                                                  1)
                                           - 1)
                                           AS OLD_PREFIX,
                                        details.new_value_str,
                                        SUBSTR (
                                           details.new_value_str,
                                           1,
                                           INSTR (details.new_value_str,
                                                  '-',
                                                  1,
                                                  1)
                                           - 1)
                                           AS NEW_PREFIX,
                                        overview.from_event_time,
                                        CASE
                                           WHEN SUBSTR (
                                                   details.old_value_str,
                                                   1,
                                                   INSTR (
                                                      details.old_value_str,
                                                      '-',
                                                      1,
                                                      1)
                                                   - 1) =
                                                   SUBSTR (
                                                      details.new_value_str,
                                                      1,
                                                      INSTR (
                                                         details.
                                                          new_value_str,
                                                         '-',
                                                         1,
                                                         1)
                                                      - 1)
                                           THEN
                                              'OK'
                                           ELSE
                                              'DELETE'
                                        END
                                           AS COMPARE
                                   FROM    audit_trail overview
                                        INNER JOIN
                                           audit_info details
                                        ON overview.serial = details.serial
                                  WHERE TO_CHAR (SYSDATE, 'IW-YYYY') =
                                           TO_CHAR (
                                              overview.from_event_time,
                                              'IW-YYYY')
                                        AND details.field_name =
                                               '__99__Department'
                                        AND overview.action = 'Update'
                                        AND CASE
                                               WHEN SUBSTR (
                                                       details.
                                                        old_value_str,
                                                       1,
                                                       INSTR (
                                                          details.
                                                           old_value_str,
                                                          '-',
                                                          1,
                                                          1)
                                                       - 1) =
                                                       SUBSTR (
                                                          details.
                                                           new_value_str,
                                                          1,
                                                          INSTR (
                                                             details.
                                                              new_value_str,
                                                             '-',
                                                             1,
                                                             1)
                                                          - 1)
                                               THEN
                                                  'OK'
                                               ELSE
                                                  'DELETE'
                                            END = 'DELETE'
                                        AND REPLACE (overview.entity_key,
                                                     'user_id=',
                                                     '') NOT LIKE
                                               'EXT%'
                                        AND details.old_value_str LIKE
                                               'INT%'
                                        AND details.new_value_str NOT LIKE
                                               'INT%'))
             AND a.user_id = b.user_id
      UNION
      SELECT 'DISCONNECT rss_user  FROM user_group WITH rss_user_name = "'
             || b.rss_user_name
             || '"  rss_name = "'
             || b.rss_name
             || '" rss_type = "'
             || b.rss_type
             || '" , ug_name = "'
             || b.ug_name
             || '";'
                AS batch,
             2 AS my_order
        FROM ent_user a, ru_ug b
       WHERE a.user_id IN (SELECT user_id
                           FROM (SELECT REPLACE (overview.entity_key,
                                                 'user_id=',
                                                 '')
                                           AS USER_ID,
                                        details.old_value_str,
                                        SUBSTR (
                                           details.old_value_str,
                                           1,
                                           INSTR (
                                              details.old_value_str,
                                              '-',
                                              1,
                                              1)
                                           - 1)
                                           AS OLD_PREFIX,
                                        details.new_value_str,
                                        SUBSTR (
                                           details.new_value_str,
                                           1,
                                           INSTR (
                                              details.new_value_str,
                                              '-',
                                              1,
                                              1)
                                           - 1)
                                           AS NEW_PREFIX,
                                        overview.from_event_time,
                                        CASE
                                           WHEN SUBSTR (
                                                   details.
                                                    old_value_str,
                                                   1,
                                                   INSTR (
                                                      details.
                                                       old_value_str,
                                                      '-',
                                                      1,
                                                      1)
                                                   - 1) =
                                                   SUBSTR (
                                                      details.
                                                       new_value_str,
                                                      1,
                                                      INSTR (
                                                         details.
                                                          new_value_str,
                                                         '-',
                                                         1,
                                                         1)
                                                      - 1)
                                           THEN
                                              'OK'
                                           ELSE
                                              'DELETE'
                                        END
                                           AS COMPARE
                                   FROM    audit_trail overview
                                        INNER JOIN
                                           audit_info details
                                        ON overview.serial =
                                              details.serial
                                  WHERE TO_CHAR (SYSDATE, 'IW-YYYY') =
                                           TO_CHAR (
                                              overview.from_event_time,
                                              'IW-YYYY')
                                        AND details.field_name =
                                               '__99__Department'
                                        AND overview.action = 'Update'
                                        AND CASE
                                               WHEN SUBSTR (
                                                       details.
                                                        old_value_str,
                                                       1,
                                                       INSTR (
                                                          details.
                                                           old_value_str,
                                                          '-',
                                                          1,
                                                          1)
                                                       - 1) =
                                                       SUBSTR (
                                                          details.
                                                           new_value_str,
                                                          1,
                                                          INSTR (
                                                             details.
                                                              new_value_str,
                                                             '-',
                                                             1,
                                                             1)
                                                          - 1)
                                               THEN
                                                  'OK'
                                               ELSE
                                                  'DELETE'
                                            END = 'DELETE'
                                        AND REPLACE (
                                               overview.entity_key,
                                               'user_id=',
                                               '') NOT LIKE
                                               'EXT%'
                                        AND details.old_value_str LIKE
                                               'INT%'
                                        AND details.new_value_str NOT LIKE
                                               'INT%'))
             AND a.user_id = b.user_id
             AND (   b.rss_type = 'TEST14'
                  OR b.rss_type = 'TTTR'
                  OR b.rss_type = 'OFFI2021'
                  OR b.rss_type = 'TEST27'
                  OR b.rss_type = 'TEST22'
                  OR b.rss_type = 'TEST26'
                  OR b.rss_type = 'TEST23'
                  OR b.rss_type = 'TEST39'
                  OR b.rss_type = 'TEST19'
                  OR b.rss_type = 'TEST12'
                  OR b.rss_type = 'TEST16'
                  OR b.rss_type = 'TEST05'
                  OR b.rss_type = 'TEST09'
                  OR b.rss_type = 'HSSO'
                  OR b.rss_type = 'TEST37'
                  OR b.rss_type = 'TEST34'
                  OR b.rss_type = 'TEST1')
      UNION
      SELECT    'DELETE rss_user WITH rss_user_name = "'
             || b.rss_user_name
             || '"  rss_name = "'
             || b.rss_name
             || '" rss_type = "'
             || b.rss_type
             || '";'
                AS batch,
             3 AS my_order
        FROM ent_user a, rss_user b
       WHERE a.user_id IN (SELECT user_id
                           FROM (SELECT REPLACE (overview.entity_key,
                                                 'user_id=',
                                                 '')
                                           AS USER_ID,
                                        details.old_value_str,
                                        SUBSTR (
                                           details.old_value_str,
                                           1,
                                           INSTR (
                                              details.old_value_str,
                                              '-',
                                              1,
                                              1)
                                           - 1)
                                           AS OLD_PREFIX,
                                        details.new_value_str,
                                        SUBSTR (
                                           details.new_value_str,
                                           1,
                                           INSTR (
                                              details.new_value_str,
                                              '-',
                                              1,
                                              1)
                                           - 1)
                                           AS NEW_PREFIX,
                                        overview.from_event_time,
                                        CASE
                                           WHEN SUBSTR (
                                                   details.
                                                    old_value_str,
                                                   1,
                                                   INSTR (
                                                      details.
                                                       old_value_str,
                                                      '-',
                                                      1,
                                                      1)
                                                   - 1) =
                                                   SUBSTR (
                                                      details.
                                                       new_value_str,
                                                      1,
                                                      INSTR (
                                                         details.
                                                          new_value_str,
                                                         '-',
                                                         1,
                                                         1)
                                                      - 1)
                                           THEN
                                              'OK'
                                           ELSE
                                              'DELETE'
                                        END
                                           AS COMPARE
                                   FROM    audit_trail overview
                                        INNER JOIN
                                           audit_info details
                                        ON overview.serial =
                                              details.serial
                                  WHERE TO_CHAR (SYSDATE, 'IW-YYYY') =
                                           TO_CHAR (
                                              overview.from_event_time,
                                              'IW-YYYY')
                                        AND details.field_name =
                                               '__99__Department'
                                        AND overview.action = 'Update'
                                        AND CASE
                                               WHEN SUBSTR (
                                                       details.
                                                        old_value_str,
                                                       1,
                                                       INSTR (
                                                          details.
                                                           old_value_str,
                                                          '-',
                                                          1,
                                                          1)
                                                       - 1) =
                                                       SUBSTR (
                                                          details.
                                                           new_value_str,
                                                          1,
                                                          INSTR (
                                                             details.
                                                              new_value_str,
                                                             '-',
                                                             1,
                                                             1)
                                                          - 1)
                                               THEN
                                                  'OK'
                                               ELSE
                                                  'DELETE'
                                            END = 'DELETE'
                                        AND REPLACE (
                                               overview.entity_key,
                                               'user_id=',
                                               '') NOT LIKE
                                               'EXT%'
                                        AND details.old_value_str LIKE
                                               'INT%'
                                        AND details.new_value_str NOT LIKE
                                               'INT%'))
             AND a.user_id = b.user_id
             AND (   b.rss_type = 'TEST14'
                  OR b.rss_type = 'TEST27'
                  OR b.rss_type = 'TEST22'
                  OR b.rss_type = 'TEST26'
                  OR b.rss_type = 'TEST23'
                  OR b.rss_type = 'TEST39'
                  OR b.rss_type = 'TEST19'
                  OR b.rss_type = 'TEST12'
                  OR b.rss_type = 'TEST16'
                  OR b.rss_type = 'TEST09'
                  OR b.rss_type = 'TEST05'
                  OR b.rss_type = 'TEST37'
                  OR b.rss_type = 'TEST34'
                  OR b.rss_type = 'TEST1')
          ORDER BY my_order;

コードを重複させずにクエリを書き直すことはできますか?

4

1 に答える 1

1

次のようなビューを作成します

create view details
as
select d.*, 
       substr(d.old_value_str, 1, instr(details.old_value_str, '-', 1, 1) - 1) as old_prefix, 
       substr(d.new_value_str, 1, instr(details.new_value_str, '-', 1, 1) - 1) as new_prefix,
       case
         when substr (d.old_value_str, 1, instr (d.old_value_str, '-', 1, 1) - 1)
               = substr (d.new_value_str, 1, instr (d.new_value_str, '-', 1, 1) - 1)
         then
           'OK'
         else
           'DELETE'
       end as compare
  from audit_info d;

(またはwith clauseビューの代わりにそれを使用してください)

次に、そのように参照します

   WHERE a.user_id IN (SELECT user_id
                       FROM (SELECT REPLACE (overview.entity_key,
                                             'user_id=',
                                             '')
                                       AS USER_ID,
                                    details.old_value_str,
                                    OLD_PREFIX,
                                    details.new_value_str,
                                    NEW_PREFIX,
                                    overview.from_event_time,
                                    COMPARE
                               FROM    audit_trail overview
                                    INNER JOIN  details
                                    ON overview.serial = details.serial
                              WHERE TO_CHAR (SYSDATE, 'IW-YYYY') =
                                       TO_CHAR (
                                          overview.from_event_time,
                                          'IW-YYYY')
                                    AND details.field_name =
                                           '__99__Department'
                                    AND overview.action = 'Update'
                                    AND CASE
                                           WHEN old_prefix =
                                                   new_prefix
                                           THEN
                                              'OK'
                                           ELSE

等..

于 2013-02-25T11:33:30.707 に答える