重写查询

Re-writing Query

总体任务:- 我需要从系统 A 中的 45 个字段中检索数据并将该数据转储到临时 table 中,然后由生成 xml 数据的 unix 进程获取要导入系统B的文件。

具体问题:检索要写入 45 个字段的数据的最佳方法是什么。大多数数据是独立的,无法使用单个语句检索。我目前检索此数据的方式如下(示例如下)

我的临时 tables 保存受影响的属性 ID,我需要为其提取数据。即 PROP_LIST_TEMP 和 ASSOC_PROP_TEMP.

    SELECT SUBSTR (pro.pro_propref, 1, 25)                                  UPRN,
(SELECT SUBSTR (adr_building, 1, 100)
               FROM addresses, address_usages
              WHERE     aus_adr_refno = adr_refno
                    AND aus_aut_far_code = 'PHYSICAL'
                    AND aus_aut_fao_code = 'PRO'
                    AND (aus_end_date IS NULL OR aus_end_date > SYSDATE)
                    AND aus_pro_refno = pro.pro_refno)
                                                                                 BUILDING_NAME,          
               (SELECT CASE
                        WHEN (adr_street_number like 'BLOC%' 
                              OR adr_street_number like '%-%'
                              OR adr_street_number like '%/%')
                        THEN NULL
                        ELSE regexp_replace (adr_street_number, '[^[:digit:]]+')
                        END
                  FROM addresses, address_usages
                  WHERE aus_adr_refno = adr_refno
                    AND aus_aut_far_code = 'PHYSICAL'
                    AND aus_aut_fao_code = 'PRO'
                    AND (aus_end_date IS NULL OR aus_end_date > SYSDATE)
                    AND aus_pro_refno = pro.pro_refno)
                                                                               STREET_NUMBER,
               (SELECT CASE 
                        WHEN (adr_street_number like 'BLOC%' 
                              OR adr_street_number like '%-%'
                              OR adr_street_number like '%/%')
                        THEN SUBSTR (adr_street_number, 1, 20)
                        ELSE  REGEXP_REPLACE (adr_street_number, '[^[:alpha:]]+', '')
                        END
                  FROM addresses, address_usages
                  WHERE aus_adr_refno = adr_refno
                    AND aus_aut_far_code = 'PHYSICAL'
                    AND aus_aut_fao_code = 'PRO'
                    AND (aus_end_date IS NULL OR aus_end_date > SYSDATE)
                    AND aus_pro_refno = pro.pro_refno)
                                                                                STREET_NUMBER_SUFFIX,
               (SELECT SUBSTR (ptv_pty_code, 1, 3)
                  FROM prop_type_values
                 WHERE ptv_refno = pro.pro_hou_ptv_refno)
                                                                               HOUSE_TYPE
FROM properties pro
         WHERE pro_refno IN (select * from PIMSS_PROP_LIST_TEMP  
                             UNION
                             select * from PIMSS_ASSOC_PROP_TEMP)
         AND  pro.pro_hou_hrv_hot_code IN 
                                       (SELECT frv_code
                                          FROM first_ref_values
                                         WHERE frv_frd_domain IN ('ASS_OWN_REF')
                                           AND frv_current_ind = 'Y');

由于 select 语句中子查询的 where 子句是相同的,您可以简单地将其拉出到 where 子句中,如下所示:

SELECT SUBSTR (pro.pro_propref, 1, 25) UPRN,
       SUBSTR (addr.adr_building, 1, 100) BUILDING_NAME,          
       CASE WHEN (addr.adr_street_number like 'BLOC%' 
                  OR addr.adr_street_number like '%-%'
                  OR addr.adr_street_number like '%/%')
                 THEN NULL
            ELSE regexp_replace (addr.adr_street_number, '[^[:digit:]]+')
       END STREET_NUMBER,
       CASE WHEN (addr.adr_street_number like 'BLOC%' 
                  OR addr.adr_street_number like '%-%'
                  OR addr.adr_street_number like '%/%')
                 THEN SUBSTR (addr.adr_street_number, 1, 20)
            ELSE  REGEXP_REPLACE (addr.adr_street_number, '[^[:alpha:]]+', '')
       END STREET_NUMBER_SUFFIX,
       (SELECT SUBSTR (ptv_pty_code, 1, 3)
          FROM prop_type_values
         WHERE ptv_refno = pro.pro_hou_ptv_refno) HOUSE_TYPE
FROM   properties pro,
       (select adr_building,
               adr_street_number
        FROM   addresses, address_usages
        WHERE  aus_adr_refno = adr_refno
        AND    aus_aut_far_code = 'PHYSICAL'
        AND    aus_aut_fao_code = 'PRO'
        AND    (aus_end_date IS NULL OR aus_end_date > SYSDATE)) addr
WHERE  pro.pro_refno = aus_pro_refno
and    pro_refno IN (select * from PIMSS_PROP_LIST_TEMP  
                     UNION
                     select * from PIMSS_ASSOC_PROP_TEMP)
AND    pro.pro_hou_hrv_hot_code IN (SELECT frv_code
                                      FROM first_ref_values
                                     WHERE frv_frd_domain IN ('ASS_OWN_REF')
                                       AND frv_current_ind = 'Y');

如果 addr 子查询可能无法返回任何行,您可能需要外连接。