0

我有一个 SQL 查询需要从 7 月 1 日到 8 月 20 日进行优化和循环cp_ex_dt字段。现在我必须为每个日期单独运行并将输出保存在 csv 文件中。有人可以建议我运行它的最佳方法吗?有什么我可以在快速 sql 中使用的东西吗?请注意我的公司不允许我运行 java。

谢谢你调查它。

    SELECT A.S_A
      ,C.I_N
      ,C.P_A_I
      ,C.P_A_I_TYPE
      ,C.C_C    
      ,'Tel'      
      ,A.T_T AS T_T_Tel      
      ,A.D_A AS D_A_Tel      
      ,A.C_E_D AS E_D_Tel
      ,A.C_P_D AS P_D_Tel      
      ,'BBG'      
      ,D.T_T AS T_T_BB      
      ,D.D_A AS D_A_BB      
      ,D.C_E_D AS E_D_BB
      ,D.C_P_D AS P_D_BB      
      ,'Ext'     
      ,E.T_T AS T_T_Ext      
      ,E.D_A AS D_A_Ext      
      ,E.C_E_D AS E_D_Ext
      ,E.C_P_D AS P_D_Ext      
FROM TO.CP_A_HIS A
    ,PA_MA.IN B
    ,SE.SE_MA C
    ,TO.CP_A_HIS D
    ,TO.CP_A_HIS E
WHERE A.T_T = 'CASHDIV'
AND A.C_S = 'PD'
AND A.S_A = C.S_A
AND A.S_I_INST = B.INST
AND A.CP_EX_DT = '1-Jul-2014'  
AND A.S_I_INST = 46 
AND A.S_A = D.S_A(+)
AND A.T_T = D.T_T(+)
AND A.CP_EX_DT = D.CP_EX_DT(+)
AND D.C_S(+) = 'PD'
AND D.S_I_INST(+) = 45 
AND (A.CP_REA = 'REG'
AND D.CP_REA(+) IN ('final','REG','REG cash','partnership dst'))
AND D.UPDATE_DATE(+) > '16-Apr-2014'
AND A.S_A = E.S_A(+)
AND A.T_T = E.T_T(+)
AND A.CP_EX_DT = E.CP_EX_DT(+)
AND E.C_S(+) = 'PD'
AND E.S_I_INST(+) = 51
AND A.CP_REA = E.CP_REA(+)

UNION
SELECT A.S_A
      ,C.I_N
      ,C.P_A_I
      ,C.P_A_I_TYPE
      ,C.C_C
      ,'Tel'      
      ,A.T_T AS T_T_Tel      
      ,A.D_A AS D_A_Tel      
      ,A.C_E_D AS E_D_Tel
      ,A.C_P_D AS P_D_Tel     
      ,'BBG'      
      ,D.T_T AS T_T_BB     
      ,D.D_A AS D_A_BB      
      ,D.C_E_D AS E_D_BB
      ,D.C_P_D AS P_D_BB      
      ,'Ext'      
      ,E.T_T AS T_T_Ext      
      ,E.D_A AS D_A_Ext      
      ,E.C_E_D AS E_D_Ext
      ,E.C_P_D AS P_D_Ext      
FROM TO.CP_A_HIS A
    ,PA_MA.IN B
    ,SE.SE_MA C
    ,TO.CP_A_HIS D
    ,TO.CP_A_HIS E
WHERE A.T_T = 'CASHDIV'
AND A.C_S = 'PD'
AND A.S_A = C.S_A
AND A.S_I_INST = B.INST
AND A.CP_EX_DT = '1-Jul-2014'  
AND A.S_I_INST = 46 
AND A.S_A = D.S_A(+)
AND A.T_T = D.T_T(+)
AND A.CP_EX_DT = D.CP_EX_DT(+)
AND D.C_S(+) = 'PD'
AND D.S_I_INST(+) = 45 
AND (A.CP_REA = 'SPECIAL'
AND D.CP_REA(+) IN ('special cash','SPECIAL'))
AND D.UPDATE_DATE(+) > '16-Apr-2014'
AND A.S_A = E.S_A(+)
AND A.T_T = E.T_T(+)
AND A.CP_EX_DT = E.CP_EX_DT(+)
AND E.C_S(+) = 'PD'
AND E.S_I_INST(+) = 51
AND A.CP_REA = E.CP_REA(+)

UNION
SELECT D.S_A
      ,C.I_N
      ,C.P_A_I
      ,C.P_A_I_TYPE
      ,C.C_C
      ,'Tel'      
      ,A.T_T AS T_T_Tel      
      ,A.D_A AS D_A_Tel      
      ,A.C_E_D AS E_D_Tel
      ,A.C_P_D AS P_D_Tel      
      ,'BBG'      
      ,D.T_T AS T_T_BB      
      ,D.D_A AS D_A_BB      
      ,D.C_E_D AS E_D_BB
      ,D.C_P_D AS P_D_BB      
      ,'Ext'      
      ,E.T_T AS T_T_Ext      
      ,E.D_A AS D_A_Ext      
      ,E.C_E_D AS E_D_Ext
      ,E.C_P_D AS P_D_Ext      
FROM TO.CP_A_HIS A
    ,PA_MA.IN B
    ,SE.SE_MA C
    ,TO.CP_A_HIS D
    ,TO.CP_A_HIS E
WHERE D.T_T = 'CASHDIV'
AND D.C_S = 'PD'
AND D.S_A = C.S_A
AND D.S_I_INST = B.INST
AND D.CP_EX_DT = '1-Jul-2014'  
AND D.S_I_INST = 45 
AND A.S_A(+) = D.S_A
AND A.T_T(+) = D.T_T
AND A.CP_EX_DT(+) = D.CP_EX_DT
AND A.C_S(+) = 'PD'
AND A.S_I_INST(+) = 46 
AND (A.CP_REA(+) = 'SPECIAL'
AND D.CP_REA IN ('special cash','SPECIAL'))
AND D.UPDATE_DATE > '16-Apr-2014'
AND A.INST IS NULL
AND D.S_A = E.S_A(+)
AND D.T_T = E.T_T(+)
AND D.CP_EX_DT = E.CP_EX_DT(+)
AND E.C_S(+) = 'PD'
AND E.S_I_INST(+) = 51
AND (E.CP_REA(+) = 'SPECIAL'
AND D.CP_REA IN ('special cash','SPECIAL'))

UNION
SELECT D.S_A
      ,C.I_N
      ,C.P_A_I
      ,C.P_A_I_TYPE
      ,C.C_C
      ,'Tel'      
      ,A.T_T AS T_T_Tel      
      ,A.D_A AS D_A_Tel      
      ,A.C_E_D AS E_D_Tel
      ,A.C_P_D AS P_D_Tel      
      ,'BBG'      
      ,D.T_T AS T_T_BB      
      ,D.D_A AS D_A_BB      
      ,D.C_E_D AS E_D_BB
      ,D.C_P_D AS P_D_BB      
      ,'Ext'     
      ,E.T_T AS T_T_Ext     
      ,E.D_A AS D_A_Ext      
      ,E.C_E_D AS E_D_Ext
      ,E.C_P_D AS P_D_Ext     
FROM TO.CP_A_HIS A
    ,PA_MA.IN B
    ,SE.SE_MA C
    ,TO.CP_A_HIS D
    ,TO.CP_A_HIS E
WHERE D.T_T = 'CASHDIV'
AND D.C_S = 'PD'
AND D.S_A = C.S_A
AND D.S_I_INST = B.INST
AND D.CP_EX_DT = '1-Jul-2014'  
AND D.S_I_INST = 45 
AND A.S_A(+) = D.S_A
AND A.T_T(+) = D.T_T
AND A.CP_EX_DT(+) = D.CP_EX_DT
AND A.C_S(+) = 'PD'
AND A.S_I_INST(+) = 46 
AND (A.CP_REA(+) = 'REG'
AND D.CP_REA IN ('final','REG','REG cash','partnership dst'))
AND D.UPDATE_DATE > '16-Apr-2014'
AND A.INST IS NULL
AND D.S_A = E.S_A(+)
AND D.T_T = E.T_T(+)
AND D.CP_EX_DT = E.CP_EX_DT(+)
AND E.C_S(+) = 'PD'
AND E.S_I_INST(+) = 51
AND (E.CP_REA(+) = 'REG'
AND D.CP_REA IN ('final','REG','REG cash','partnership dst'))
4

1 回答 1

0

除了与您的“D”别名相关的“REG”和“SPECIAL”条目之外,您的前两部分联合查询几乎相同。这两个可以通过使用第一个选择来简化,但在以下位置更改区域:

  AND D.S_I_INST(+) = 45 
  AND (
         (     A.CP_REA = 'REG'
           AND D.CP_REA(+) IN ('final','REG','REG cash','partnership dst')
         )
         OR
         (    A.CP_REA = 'SPECIAL'
          AND D.CP_REA(+) IN ('special cash','SPECIAL')
         )
       )

并将完全删除针对“A”表的第一个联合。

您的第三个和第四个联合是相似的,但从您的“A”表为 NULL 的“D”表中提取。将它的 where 子句更改为:

  AND A.S_I_INST(+) = 46 
  AND (
        (     A.CP_REA(+) = 'REG'
          AND D.CP_REA IN ('final','REG','REG cash','partnership dst')
        )
        OR
        (     A.CP_REA(+) = 'SPECIAL'
          AND D.CP_REA IN ('special cash','SPECIAL')
        )
      )

所以这应该导致简化

select fields
   from your "A" based query
union
select fields
   from your "D" based query

此外,它可能会被进一步简化,但在没有看到更多数据上下文的情况下,“A”和“D”表的自连接可能会消除联合,但不确定当前是否存在。

根据反馈进行编辑 不是 Oracle 专家,但确实认识到 (+) 是左连接语法的指示符......并且没有 oracle 可以尝试使用实际的示例表和数据,我只能提供以下轻微调整。看来您正在处理每当“A”是“REG”时,“D.CP_REA”的唯一可能性就是提供的 IN 列表,并且永远不会有“A.CP_CREA”作为“REG”而“D”方有“特殊现金”或“特殊”……反之亦然。如果是这样,也许你可以适应

  AND D.S_I_INST(+) = 45 
  AND A.CP_REA IN ( 'REG', 'SPECIAL' )
  AND D.CP_REA(+) IN ( 'final','REG','REG cash','partnership dst', 
                       'special cash','SPECIAL')
于 2014-08-25T13:33:58.867 回答