如何根据表中的其他4列获取唯一实例(列)

时间:2017-12-01 23:07:50

标签: sql

我想从目前不在下面查询中的表中添加一个名为Order_No的新列:ordloc_discount

这四列是

deal_id
item
location
last_calc_date


SELECT idh.vendor supplier
       ,idh.deal_id
      ,mff_report.mff_merch_sql.get_sup_name(idh.vendor)  sup_name
      ,idh.deal_type
      ,scdhh.start_invoice_date
      ,TO_CHAR(scdhh.start_invoice_date,'MM/DD/YYYY') start_invoice_date_char
      ,idh.doc_date
      ,TO_CHAR(idh.doc_date,'MM/DD/YYYY') doc_date_char
      ,idh.ext_doc_id
      ,NULL deal_desc
      ,idh.custom_doc_ref_2 ext_ref_no
      ,idh.custom_doc_ref_3 deal_comp_type_desc
      ,idh.custom_doc_ref_4 comments
      ,idh.total_qty
      ,idh.total_cost
                      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  NULL
                         WHEN 'VL' THEN  icdd.loc_type
                         WHEN 'P'  THEN  NULL
                         WHEN 'PL' THEN  icdd.loc_type
                       ELSE  icdd.loc_type
                       END loc_type
                      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  NULL
                         WHEN 'VL' THEN  icdd.location
                         WHEN 'P'  THEN  NULL
                         WHEN 'PL' THEN  icdd.location
                       ELSE  icdd.location
                       END location 
                      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  NULL
                         WHEN 'VL' THEN  vl.loc_name
                         WHEN 'P'  THEN  NULL
                         WHEN 'PL' THEN  vl.loc_name
                       ELSE  vl.loc_name
                       END loc_name 
                      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  NULL
                         WHEN 'VL' THEN  NULL
                         WHEN 'P'  THEN  NVL(im_parent.item,im.item)
                         WHEN 'PL' THEN  NVL(im_parent.item,im.item)
                       ELSE im.item
                       END item 
                      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  NULL
                         WHEN 'VL' THEN  NULL
                         WHEN 'P'  THEN  NVL(mff_report.mff_merch_sql.get_brand_name(NVL(im_parent.item,im.item)),' ')
                         WHEN 'PL' THEN  NVL(mff_report.mff_merch_sql.get_brand_name(NVL(im_parent.item,im.item)),' ')
                       ELSE NVL(mff_report.mff_merch_sql.get_brand_name(im.item),' ')
                       END brand_name
                      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  NULL
                         WHEN 'VL' THEN  NULL
                         WHEN 'P'  THEN  NVL(im_parent.item_desc,im.item_desc)
                         WHEN 'PL' THEN  NVL(im_parent.item_desc,im.item_desc)
                       ELSE  im.item_desc
                       END item_desc
                      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
                         WHEN 'VL' THEN  mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
                         WHEN 'P'  THEN  mff_report.mff_merch_sql.get_vpn_for_item_vendor(NVL(im_parent.item,im.item),'S',TO_NUMBER(idh.vendor))
                         WHEN 'PL' THEN  mff_report.mff_merch_sql.get_vpn_for_item_vendor(NVL(im_parent.item,im.item),'S',TO_NUMBER(idh.vendor))
                       ELSE  mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
                       END vpn
      ,SUM(icdd.actual_turnover_units) turnover_units
      ,SUM(icdd.actual_turnover_revenue) turnover_revenue
      ,SUM(icdd.income_deal_curr) income
  FROM im_doc_head idh
      ,mff_report.stage_complex_deal_head_hist scdhh
      ,im_complex_deal_detail icdd
      ,mff_report.v_loc vl
      ,item_master im
                      ,item_master im_parent
 WHERE (   (    idh.type IN ('DEBMEC','CRDMEC') --Debit and Credit Memos in APPROVED or POSTED
            AND idh.status IN ('APPRVE','POSTED'))
        OR (    idh.type = 'CRDNRC'             --Credit Note Requests in APPROVED or MATCHED
            AND idh.status IN ('APPRVE','MTCH')))
   AND idh.deal_type = 'C'
   AND NVL(:PM_supplier,idh.vendor) = idh.vendor
   AND idh.doc_date BETWEEN NVL(:PM_doc_date_from,idh.doc_date) AND NVL(:PM_doc_date_to,idh.doc_date)
   AND TRUNC(idh.approval_date) BETWEEN NVL(:PM_approval_date_from,TRUNC(idh.approval_date)) AND NVL(:PM_approval_date_to,TRUNC(idh.approval_date))
   AND (   (    :PM_edi_812_filter = 'Y'
            AND NOT EXISTS (SELECT 1
                              FROM sups s
                                  ,sup_traits_matrix stm
                             WHERE TO_NUMBER(idh.vendor) = s.supplier_parent
                               AND s.supplier = stm.supplier
                               AND stm.sup_trait = 525))
        OR :PM_edi_812_filter = 'N'
        OR :PM_supplier IS NOT NULL) -- ignore EDI 812 filter if a supplier is selected
   AND (   idh.ext_doc_id MEMBER OF mff_report.parse_strings(:PM_ext_doc_id)
        OR :PM_ext_doc_id IS NULL)
   AND (   :PM_batch_mode = 'N'
        OR (    :PM_batch_mode = 'Y' -- batch mode, pick up un-downloaded docs, and exclude matched CNRs which will not be downloaded
            AND idh.edi_download_ind = 'N'
            AND idh.status != 'MTCH'))
   AND idh.deal_id = scdhh.deal_id (+)
   AND SUBSTR(idh.ext_doc_id,(INSTR(idh.ext_doc_id,'-',1) + 1),INSTR(idh.ext_doc_id,'-',1,2) - (INSTR(idh.ext_doc_id,'-',1) + 1)) = scdhh.deal_detail_id (+)
   AND idh.doc_date = scdhh.end_invoice_date (+)
   AND idh.doc_id = icdd.doc_id
   AND icdd.location = vl.loc
   AND icdd.item = im.item
                   AND im.item_parent = im_parent.item (+)
   AND (   :PM_supplier IS NOT NULL
        OR :PM_doc_date_from IS NOT NULL
        OR :PM_doc_date_to IS NOT NULL
        OR :PM_approval_date_from IS NOT NULL
        OR :PM_approval_date_to IS NOT NULL
        OR :PM_ext_doc_id IS NOT NULL
        OR :PM_batch_mode = 'Y')
 GROUP BY idh.vendor
      ,idh.deal_id
      ,mff_report.mff_merch_sql.get_sup_name(idh.vendor)
      ,idh.deal_type
      ,scdhh.start_invoice_date
      ,TO_CHAR(scdhh.start_invoice_date,'MM/DD/YYYY')
      ,idh.doc_date`enter code here`
      ,TO_CHAR(idh.doc_date,'MM/DD/YYYY')
      ,idh.ext_doc_id
      ,idh.custom_doc_ref_2
      ,idh.custom_doc_ref_3
      ,idh.custom_doc_ref_4
      ,idh.total_qty
      ,idh.total_cost
      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  NULL
                         WHEN 'VL' THEN  icdd.loc_type
                         WHEN 'P'  THEN  NULL
                         WHEN 'PL' THEN  icdd.loc_type
                       ELSE  icdd.loc_type
                       END 
                      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  NULL
                         WHEN 'VL' THEN  icdd.location
                         WHEN 'P'  THEN  NULL
                         WHEN 'PL' THEN  icdd.location
                       ELSE  icdd.location
                       END 
                      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  NULL
                         WHEN 'VL' THEN  vl.loc_name
                         WHEN 'P'  THEN  NULL
                         WHEN 'PL' THEN  vl.loc_name
                       ELSE  vl.loc_name
                       END 
                      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  NULL
                         WHEN 'VL' THEN  NULL
                         WHEN 'P'  THEN  NVL(im_parent.item,im.item)
                         WHEN 'PL' THEN  NVL(im_parent.item,im.item)
                       ELSE im.item
                       END 
                      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  NULL
                         WHEN 'VL' THEN  NULL
                         WHEN 'P'  THEN  NVL(mff_report.mff_merch_sql.get_brand_name(NVL(im_parent.item,im.item)),' ')
                         WHEN 'PL' THEN  NVL(mff_report.mff_merch_sql.get_brand_name(NVL(im_parent.item,im.item)),' ')
                       ELSE NVL(mff_report.mff_merch_sql.get_brand_name(im.item),' ')
                       END 
                      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  NULL
                         WHEN 'VL' THEN  NULL
                         WHEN 'P'  THEN  NVL(im_parent.item_desc,im.item_desc)
                         WHEN 'PL' THEN  NVL(im_parent.item_desc,im.item_desc)
                       ELSE  im.item_desc
                       END 
                      ,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
                         WHEN 'V'  THEN  mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
                         WHEN 'VL' THEN  mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
                         WHEN 'P'  THEN  mff_report.mff_merch_sql.get_vpn_for_item_vendor(NVL(im_parent.item,im.item),'S',TO_NUMBER(idh.vendor))
                         WHEN 'PL' THEN  mff_report.mff_merch_sql.get_vpn_for_item_vendor(NVL(im_parent.item,im.item),'S',TO_NUMBER(idh.vendor))
                       ELSE  mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
                       END 

从当前不在查询中的表中获取新列order_no

0 个答案:

没有答案