Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Avoid duplicate entries in a SQL join query

$
0
0

Hi,
Am using this query for PO purchase requisition.
I am using joins in the query and it is retrieving duplicate records and it takes 2 hrs to complete the query. Is there any way to avoid the duplicate records and to reduce the execution time. I tried using distinct but it a long time to execute. Help me with this .
The query i used

     SELECT  porl.REQUISITION_HEADER_ID,
        porh.REQUISITION_NUMBER,
        porl.REQUISITION_LINE_ID,
        porl.LINE_NUM REQUISITION_LINE_NUM,
        porl.LINE_TYPE_ID,
        plt.LINE_TYPE,
        porl.ITEM_ID,
        porl.ORG_ID,
       porh.OPERATING_UNIT_NAME,
       msi.SEGMENT1 ITEM_NUMBER,
       porl.ITEM_REVISION,
       porl.CATEGORY_ID,
       mcb.SEGMENT1 CATEGORY_SEGMENT1,
       mcb.SEGMENT2 CATEGORY_SEGMENT2,
       mcb.STRUCTURE_ID CATEGORY_STRUCTURE_ID,
       fs.ID_FLEX_STRUCTURE_CODE CATEGORY_STRUCTURE_CODE,
       fs.ID_FLEX_CODE CATEGORY_FLEX_CODE,
       porl.ITEM_DESCRIPTION,
       porl.UNIT_MEAS_LOOKUP_CODE UOM,
       muom.UOM_CODE,
       porl.QUANTITY,
       porl.UNIT_PRICE,
       porl.NEED_BY_DATE,
       porl.AMOUNT,
       porl.NOTE_TO_RECEIVER,
       porl.NOTE_TO_AGENT,
       porl.NOTE_TO_VENDOR,
       porl.SUGGESTED_BUYER_ID,
       ppf.EMPLOYEE_NUMBER SUGGESTED_BUYER_EMPNUM,
       ppf.LAST_NAME SUGGESTED_BUYER_LASTNAME,
       ppf.FIRST_NAME SUGGESTED_BUYER_FIRSTNAME,
       ppf.FULL_NAME SUGGESTED_BUYER_FULLNAME,
       porl.RFQ_REQUIRED_FLAG,
       porl.SUGGESTED_VENDOR_PRODUCT_CODE SUPPLIER_ITEM,
       porl.JUSTIFICATION,
       porl.TRANSACTION_REASON_CODE,
       porl.REFERENCE_NUM,
       porl.UN_NUMBER_ID,
       poun.UN_NUMBER,
       porl.HAZARD_CLASS_ID,
       phc.HAZARD_CLASS,
       porl.CURRENCY_CODE,
       porl.RATE_TYPE,
       porl.RATE_DATE,
       porl.RATE,       
       porl.BASE_UNIT_PRICE,
       porl.CURRENCY_AMOUNT,
       porl.CURRENCY_UNIT_PRICE,
       porl.DOCUMENT_TYPE_CODE,
       porl.SOURCE_TYPE_CODE,
       porl.DESTINATION_TYPE_CODE,
       porl.SOURCE_ORGANIZATION_ID,
       ood1.ORGANIZATION_NAME SOURCE_ORGANIZATION_NAME,
       ood1.ORGANIZATION_CODE SOURCE_ORGANIZATION_CODE,       
       porl.SOURCE_SUBINVENTORY,
       porl.TO_PERSON_ID REQUESTOR_PERSON_ID,
       ppf1.EMPLOYEE_NUMBER REQUESTOR_EMPLOYEE_NUMBER,
       ppf1.LAST_NAME REQUESTOR_LAST_NAME,
       ppf1.FIRST_NAME REQUESTOR_FIRST_NAME,
       ppf1.FULL_NAME REQUESTOR_FULL_NAME,
       porl.DESTINATION_ORGANIZATION_ID,
       ood2.ORGANIZATION_NAME DESTINATION_ORGANIZATION_NAME,
       ood2.ORGANIZATION_CODE DESTINATION_ORGANIZATION_CODE,
       porl.DELIVER_TO_LOCATION_ID,
       hla.LOCATION_CODE,
       porl.DESTINATION_SUBINVENTORY,
       porl.VENDOR_ID,
       pv.VENDOR_NAME,
       pv.SEGMENT1 VENDOR_NUMBER,
       porl.VENDOR_SITE_ID,
       pvs.VENDOR_SITE_CODE,
       porl.VENDOR_CONTACT_ID,
       pvc.LAST_NAME VENDOR_CONTACT_LASTNAME,
       pvc.FIRST_NAME VENDOR_CONTACT_FIRSTNAME,
       porl.SUGGESTED_VENDOR_PHONE VENDOR_PHONE,
       porl.ORDER_TYPE_LOOKUP_CODE,
       porl.MATCHING_BASIS,
       porl.PURCHASE_BASIS       
FROM PO_REQUISITION_LINES_ALL porl,
     XX_PO_REQ_HEADER_EXTR_STG porh,
     PO_LINE_TYPES_TL plt,
     MTL_SYSTEM_ITEMS msi,
     MTL_ITEM_REVISIONS mir,
     MTL_CATEGORIES_B mcb,
     FND_ID_FLEX_STRUCTURES fs,
     MTL_UNITS_OF_MEASURE_TL muom,
     PER_ALL_PEOPLE_F ppf,
     PER_ALL_PEOPLE_F ppf1,
     PO_LOOKUP_CODES plc1,
     PO_LOOKUP_CODES plc2,
     PO_LOOKUP_CODES plc3,
     PO_LOOKUP_CODES plc4,
     PO_UN_NUMBERS poun,
     PO_HAZARD_CLASSES phc,
     FND_CURRENCIES fc,
     GL_DAILY_CONVERSION_TYPES gdct,
     ORG_ORGANIZATION_DEFINITIONS ood1,
     ORG_ORGANIZATION_DEFINITIONS ood2,
     MTL_SECONDARY_INVENTORIES msiv1,
     MTL_SECONDARY_INVENTORIES msiv2,
     HR_LOCATIONS_ALL hla,
     PO_VENDORS pv,
     PO_VENDOR_SITES_ALL pvs,
     PO_VENDOR_CONTACTS pvc
WHERE porh.REQUISITION_HEADER_ID = porl.REQUISITION_HEADER_ID
AND porl.LINE_TYPE_ID = plt.LINE_TYPE_ID
AND porl.ITEM_ID = msi.INVENTORY_ITEM_ID(+)
AND porh.ORG_ID = nvl(msi.ORGANIZATION_ID,porh.ORG_ID)
AND porl.ITEM_REVISION = mir.REVISION(+)
AND porl.ITEM_ID = mir.INVENTORY_ITEM_ID(+)
AND porh.ORG_ID = nvl(mir.ORGANIZATION_ID,porh.ORG_ID)
AND porl.CATEGORY_ID = mcb.CATEGORY_ID
AND mcb.ENABLED_FLAG = 'Y'
AND nvl(mcb.START_DATE_ACTIVE,SYSDATE) <= SYSDATE
AND nvl(mcb.END_DATE_ACTIVE,SYSDATE) >= SYSDATE
AND nvl(mcb.DISABLE_DATE,SYSDATE) >= SYSDATE
AND mcb.STRUCTURE_ID = fs.ID_FLEX_NUM
AND porl.UNIT_MEAS_LOOKUP_CODE = muom.UNIT_OF_MEASURE
AND porl.SUGGESTED_BUYER_ID = ppf.PERSON_ID(+)
AND nvl(ppf.EMPLOYEE_NUMBER,1) IS NOT NULL
AND SYSDATE BETWEEN nvl(ppf.EFFECTIVE_START_DATE,SYSDATE) AND nvl(ppf.EFFECTIVE_END_DATE,SYSDATE)
AND porl.TRANSACTION_REASON_CODE = plc1.lookup_code(+)
AND plc1.lookup_type(+) = 'TRANSACTION REASON'
AND nvl(plc1.ENABLED_FLAG,'Y') = 'Y'
AND NVL(plc1.INACTIVE_DATE,SYSDATE) >= SYSDATE
AND porl.UN_NUMBER_ID = poun.UN_NUMBER_ID(+)
AND porl.HAZARD_CLASS_ID = phc.HAZARD_CLASS_ID(+)
AND porl.CURRENCY_CODE = fc.CURRENCY_CODE(+)
AND porl.RATE_TYPE = gdct.CONVERSION_TYPE(+)
AND porl.DOCUMENT_TYPE_CODE = plc2.LOOKUP_CODE(+)
AND plc2.LOOKUP_TYPE(+) = 'SOURCE DOCUMENT TYPE'
AND nvl(plc2.ENABLED_FLAG,'Y') = 'Y'
AND NVL(plc2.INACTIVE_DATE,SYSDATE) >= SYSDATE
AND porl.SOURCE_TYPE_CODE = plc3.LOOKUP_CODE
AND plc3.LOOKUP_TYPE = 'REQUISITION SOURCE TYPE'
AND plc3.ENABLED_FLAG = 'Y'
AND NVL(plc3.INACTIVE_DATE,SYSDATE) >= SYSDATE
AND plc4.LOOKUP_CODE = porl.DESTINATION_TYPE_CODE  
AND plc4.LOOKUP_TYPE = 'DESTINATION TYPE'
AND plc4.ENABLED_FLAG = 'Y'
AND NVL(plc4.INACTIVE_DATE,SYSDATE) >= SYSDATE
AND porl.SOURCE_ORGANIZATION_ID = ood1.ORGANIZATION_ID(+)
AND porl.SOURCE_SUBINVENTORY = msiv1.SECONDARY_INVENTORY_NAME(+)
AND porl.SOURCE_ORGANIZATION_ID = msiv1.ORGANIZATION_ID(+)
AND porl.TO_PERSON_ID = ppf1.PERSON_ID(+)
AND ppf1.EMPLOYEE_NUMBER IS NOT NULL
AND SYSDATE BETWEEN ppf1.EFFECTIVE_START_DATE AND ppf1.EFFECTIVE_END_DATE
AND porl.DESTINATION_ORGANIZATION_ID = ood2.ORGANIZATION_ID
AND porl.DELIVER_TO_LOCATION_ID = hla.LOCATION_ID
AND porl.DESTINATION_ORGANIZATION_ID = hla.INVENTORY_ORGANIZATION_ID(+)
AND NVL(hla.INACTIVE_DATE,SYSDATE) >= SYSDATE
AND porl.DESTINATION_SUBINVENTORY = msiv2.SECONDARY_INVENTORY_NAME(+)
AND porl.DESTINATION_ORGANIZATION_ID = msiv2.ORGANIZATION_ID(+)
AND porl.VENDOR_ID = pv.VENDOR_ID(+)
AND porl.VENDOR_SITE_ID = pvs.VENDOR_SITE_ID(+)
AND porl.VENDOR_CONTACT_ID = pvc.VENDOR_CONTACT_ID(+);


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>