试图更好地理解SQL查询

时间:2015-05-21 13:01:00

标签: sql

我是公司IT部门的新开发人员。我觉得完全失去了很多在我们的代码中编写的sql。有许多查询长达数百行。这是"较小"的一个例子。一个(表/字段名称已被修改,但结构相同)。我只是吮吸这个还是真的很难理解?您如何重写它以使其更易于管理/可理解?

SELECT pd.commission_header_id COMMISSION_HEADER_ID
          ,pd.sales_rep_id DIRECT_SALESREP_ID
          ,pd.org_id ORG_ID
          ,LEAST(100, innview.spilt_percent)  SPLIT_PCT
          ,pd.source SOURCE
          ,pd.application APPLICATION
          ,pd.plan PLANE
          ,pd.pay_freq pay_freq
          ,pd.emp_app_count EMP_APP_COUNT
          ,pd.client_name CLIENT_NAME
          ,pd.fed_id FED_ID
          ,pd.off_nbr OFF_NBR
          ,pd.payroll_num payroll_num
          ,TO_DATE(pd.product_start_date,'DD-MON-YY') PRODUCT_START_DATE
          ,pd.loss_date LOSS_DATE
          ,pd.acquisitions_ind ACQUISITIONS_IND
          ,pd.source_of_business SOURCE_OF_BUSINESS
          ,pd.prev_br_clt_nbr PREV_BR_CLT_NBR
          ,pd.previous_method PREVIOUS_METHOD
          ,pd.natl_acct_number NATL_ACCT_NUMBER
          ,pd.product_name PRODUCT_NAME
          ,pd.lost_reason LOST_REASON
          ,pd.client_id CLIENT_ID
          ,pd.lead_nbr LEAD_NBR
          ,pd.processed_date PROCESSED_DATE
          ,pd.rep_type REP_TYPE
          ,pd.order_number ORDER_NUMBER 
          ,pd.conversion_type CONVERSION_TYPE
          ,SUBSTR(original_billing_info,1,INSTR(original_billing_info,'|',1,1)-1) BILLING_APPLICATION
          ,SUBSTR(original_billing_info,INSTR(original_billing_info,'|',1,1)+1,INSTR(original_billing_info,'|',1,2)-INSTR(original_billing_info,'|',1,1)-1) BILLING_CODE
          ,SUBSTR(original_billing_info,INSTR(original_billing_info,'|',1,2)+1,INSTR(original_billing_info,'|',1,3)-INSTR(original_billing_info,'|',1,2)-1) BILLING_PROD_GRP_NM
          ,SUBSTR(original_billing_info,INSTR(original_billing_info,'|',1,3)+1) BILLING_FIELD_FOR_CHRGS
          ,SUBSTR(original_discount_info,1,INSTR(original_discount_info,'|',1,1)-1) DISCOUNT_APPLICATION
          ,SUBSTR(original_discount_info,INSTR(original_discount_info,'|',1,1)+1,INSTR(original_discount_info,'|',1,2)-INSTR(original_discount_info,'|',1,1)-1) DISCOUNT_CODE
          ,CASE WHEN (NVL(innview.trans,0)) = 0 
                THEN 0 
                ELSE ( NVL(innview.comm,0)) / ( NVL(innview.trans,0)) 
                 END ORIGINAL_COMM_PCT 
          ,pd.record_type RECORD_TYPE 
          ,innview.trans PAR_SUM 
          ,NVL(ocp.oic_clients_pk, pxmis.oic_client_products_pk_seq.nextval) CLIENT_PRODUCT_ID
          ,pd.associated_with ASSOCIATED_WITH_ID
          ,pd.refferal_sale refferal_sale
          ,pd.parent_client_nbr PARENT_CLIENT_NBR
      FROM pxmis.pd_commissions pd
      JOIN pxmis.oic_lookback_months olm ON (pd.application = olm.application
                                           AND pd.plan = olm.plan) 
      LEFT OUTER JOIN pxmis.oic_client_products ocp ON (pd.off_nbr = ocp.off_nbr
                                             AND pd.payroll_num = ocp.payroll_num
                                             AND pd.application = ocp.application
                                             AND pd.plan = ocp.plan
                                             AND pd.sales_rep_id = ocp.direct_salesrep_id
                                             AND pd.source = ocp.source
                                             AND ocp.record_type in (:1,:2)
                                             AND NVL(pd.refferal_sale,'NON REF') = NVL(ocp.refferal_sale,'NON REF'))
       JOIN (SELECT SUM(NVL(transaction_amount,0)) as trans 
                   ,SUM(commission_amount) as comm 
                   ,SUM(pd2.oic_split_percent) as spilt_percent 
                   ,pd2.application as application 
                   ,pd2.plan       as code 
                   ,pd2.sales_rep_id    as sales_rep_id 
                   ,pd2.payroll_num as client_nbr 
                   ,LPAD(pd2.off_nbr,4,0) as off_nbr 
               FROM pxmis.pd_commissions pd2 
               JOIN pxmis.oic_lookback_months olm2 ON (pd2.application = olm2.application 
                                                   AND pd2.plan = olm2.plan) 
               WHERE CASE WHEN (pd2.record_type IN ('SAPBA','ASAPBA')) 
                          THEN pd2.SUB_RECORD_TYPE 
                          ELSE 'Eligible' 
                          END = 'Eligible' 
                 AND pd2.record_type IN ('SDBDA', 'ASDBDA', 
                                          decode(:3,'CHCST','SAPBA',NULL), 
                                          decode(:4,'CHCST','ASAPBA',NULL), 
                                          'SAPBP','ASAPBP') 
                AND pd2.processed_date BETWEEN ADD_MONTHS(:5,(olm2.lookback_months_high - 1) * -1) AND ADD_MONTHS(:6, (olm2.lookback_months_low - 2) * -1) - 1 
                and :7 between olm2.effective_start_date and olm2.effective_end_date 
                and olm2.function_type = :8 
            GROUP BY pd2.application  
                  ,pd2.plan  
                  ,pd2.sales_rep_id  
                  ,pd2.payroll_num  
                  ,LPAD(pd2.off_nbr,4,0)) innview on (innview.application = pd.application 
                                              AND innview.code = pd.plan 
                                              AND innview.sales_rep_id = pd.sales_rep_id 
                                              AND innview.client_nbr = pd.payroll_num 
                                              AND innview.off_nbr = LPAD(pd.off_nbr,4,0)) 
     WHERE :9 BETWEEN olm.effective_start_date AND olm.effective_end_date
       AND pd.sales_rep_id <> -3
       AND pd.record_type IN ('SAPBP','ASAPBP')
       AND NOT EXISTS(SELECT 'X'
                        FROM pxmis.pd_commissions pd3
                       WHERE pd3.application = pd.application
                         AND pd3.plan = pd.plan
                         AND pd3.sales_rep_id = pd.sales_rep_id
                         AND pd3.payroll_num = pd.payroll_num
                         AND (pd3.record_type in ('CHCST','ACHCST') 
                              or ( pd3.record_type = 'SAPBA' 
                                   and pd3.sub_record_type = 'Chargeback')))
       AND pd.commission_header_id = (SELECT MAX(pd4.commission_header_id)
                                    FROM pxmis.pd_commissions pd4
                                   WHERE pd4.application = pd.application
                                     AND pd4.plan = pd.plan
                                     AND pd4.sales_rep_id = pd.sales_rep_id
                                     AND pd4.payroll_num = pd.payroll_num
                                     AND pd4.record_type = pd.record_type) 
       AND olm.function_type = :10
       AND pd.processed_date BETWEEN ADD_MONTHS(:11,(olm.lookback_months_high - 1) * -1) AND ADD_MONTHS(:12, (olm.lookback_months_low - 2) * -1) - 1
       AND pd.source = :13 
       AND pay_freq IS NOT NULL
       AND pd.core_conversion_revenue IS NULL;

1 个答案:

答案 0 :(得分:0)

旁注:(由于我的声誉,我无法发表评论:)

这是我第一次回答。希望它会有所帮助。 我不知道它是否有帮助,但大多数SQL脚本(可能全部)都可以用其他语言重写。例如,您可以在C#.NET LINQ中使用 JOINS

但我通常将SQL脚本分开的方法是查找关键字并查看它们的作用。不要混淆小字母的大字母。

SELECT 功能选择所有数据。

TO_DATE 函数表示它会将字符串转换为日期(来源:http://www.techonthenet.com/oracle/functions/to_date.php

LEAST 函数返回您作为参数的变量之间的最小数字(来源:http://www.techonthenet.com/oracle/functions/least.php

您可以为每个功能执行此操作,并尝试掌握脚本的功能。

我认为你可以在这里结束脚本:FROM pxmis.pd_commissions pd

我也看到你做了 JOIN 。 在这里,您可以正确地创建一个新脚本并运行该代码。

现在我可能无法使其更具可读性。 (主要是因为我使用.NET C#linqs。)

希望它在某种程度上有所帮助。