在 Create View 中优化基于 RANK 的查询

Optimise RANK based query in Create View

我有一个观点,为了使用 RANK 函数,我使用了 2 个 select 个案例。 有没有办法优化它并避免 2 select 子句并使其成为一个。

查询如下:

CREATE OR REPLACE VIEW "TEMP" (
   "P_LOCAL_POLICY_ID",
   "P_GIRDA_POLICY_ID",
   "C_OE_CONS_UNIT_TITLE",
   "C_ROW_NUMBER",
   "C_LOAD_TIMESTAMP",
   "ERROR_TEXT",
   "RECORD_2_DELETE",
   "ERROR_TIMESTAMP",
   "RANK_NUMBER"
) AS
   SELECT
       p_local_policy_id,
       p_girda_policy_id,
       c_oe_cons_unit_title,
       c_row_number,
       c_load_timestamp,
       err_error_text,
       err_record_2_delete,
       err_load_timestamp,
       RANK() OVER(PARTITION BY
           err_error_text
           ORDER BY
               ROWNUM
       )
   FROM
       (
           SELECT
               po.local_policy_id AS p_local_policy_id,
               po.girda_policy_id AS p_girda_policy_id,
               MAX(cl.oe_cons_unit_title) AS c_oe_cons_unit_title,
               SUM(cl.row_number) AS c_row_number,
               MAX(cl.load_timestamp) AS c_load_timestamp,
               error.error_text AS err_error_text,
               error.record_2_delete AS err_record_2_delete,
               error.load_timestamp AS err_load_timestamp
           FROM
              <condition>
           WHERE
                  <condition>
           GROUP BY
               po.local_policy_id,
               po.girda_policy_id,
               error.error_text,
               error.record_2_delete,
               error.load_timestamp
       );   

谢谢, 奇特拉

您可以将 SELECT 列表的两个块合并为

CREATE OR REPLACE VIEW TEMP AS
SELECT po.local_policy_id AS "P_LOCAL_POLICY_ID",
       po.girda_policy_id AS "P_GIRDA_POLICY_ID",
       MAX(cl.oe_cons_unit_title) AS "C_OE_CONS_UNIT_TITLE",
       SUM(cl.row_number) AS "C_ROW_NUMBER",
       MAX(cl.load_timestamp) AS "C_LOAD_TIMESTAMP",
       error.error_text AS "ERROR_TEXT",
       error.record_2_delete AS "RECORD_2_DELETE",
       error.load_timestamp AS "ERROR_TIMESTAMP",
       RANK() OVER( PARTITION BY err_error_text ORDER BY 1 ) AS "RANK_NUMBER"
  FROM <condition>
 WHERE <condition>
 GROUP BY po.local_policy_id, po.local_region,
          error.error_text, error.record_2_delete, error.load_timestamp
  • 无需在视图名称后列出这些列别名。
  • 列名称中的下划线字符可能会被删除,例如 "P LOCAL POLICY ID" 而不是 "P_LOCAL_POLICY_ID" 被引用