speed up and lean up my SQL Statement that will be come a view

  • Will you look at my SQL Statement and give me ideas on how to speed it up and make it leaner. It will be made into a view to report off of anf it will be used a lot.

    SELECT DISTINCT OH.ORDER_NO

    ,OH.CUSTOMER_PO

    ,OH.PYMNT_TERMS_CD

    ,OL.PRODUCT_ID

    ,OL.PRICE_PROTECTED

    , SHIP_CUST.CUST_ID SHIP_TO_CUST_ID

    , BILL_CUST.CUST_ID BILL_TO_CUST_ID

    , SHIP_CUST.SHIP_CUST_NAME1 SHIP_TO_NAME

    , BILL_CUST.NAME1 BILL_TO_NAME

    , BILL_CUST.CITY BILL_TO_CITY

    , SHIP_CUST.CITY SHIP_TO_CITY

    , SHIP_CUST.ADDRESS1 SHIP_TO_ADDRESS1

    , BILL_CUST.ADDRESS1 BILL_TO_ADDRESS

    , SHIP_CUST.STATE SHIP_TO_STATE

    , BILL_CUST.STATE BILL_TO_STATE

    , SHIP_CUST.POSTAL SHIP_TO_POSTAL

    , BILL_CUST.POSTAL BILL_TO_POSTAL

    , SHIP_CUST.PHONE PHONE

    , BILL_CUST.PHONE PHONE2

    ,ID.PARENT_PROD_ID

    ,ID.DP_KIT_PARENT_QTY

    ,ID.KIT_LINE_NBR

    ,ID.QTY_PER

    ,ID.INV_ITEM_ID

    ,ID.QTY_REQUESTED

    ,ID.QTY_BACKORDER

    ,ID.QTY_SHIPPED

    ,ID.LOAD_ID

    ,ID.TMS_EXT_REF_ID

    ,ID.ORDER_INT_LINE_NO

    ,OH.SUPPORT_TEAM_CD

    ,OH.CARRIER_ID

    ,OH.OPRID_ENTERED_BY

    ,OH.ORDER_DATE

    ,OH.ORDER_GRP

    ,OH.REQ_ARRIVAL_DTTM

    ,(

    SELECT SUM(TAX_AMT)

    FROM PS_ORD_SCHEDULE OS1

    WHERE OS1.BUSINESS_UNIT=ID.BUSINESS_UNIT

    AND OS1.ORDER_NO = ID.ORDER_NO

    AND OS1.ORDER_INT_LINE_NO=ID.ORDER_INT_LINE_NO ) AS TAX_AMT

    ,(

    SELECT DISTINCT ES.FRT_CHRG_OVERRIDE

    FROM PS_ORD_EST_SHIP ES

    WHERE ES.BUSINESS_UNIT=ID.BUSINESS_UNIT

    AND ES.ORDER_NO=ID.ORDER_NO

    AND ES.FRT_CHRG_OVERRIDE='Y' ) AS FRT_CHRG_OVERRIDE ,OL.PRICE

    , (

    SELECT DISTINCT TRFT_SET_ID

    FROM PS_ORD_FRT_CHRG

    WHERE TRFT_SET_ID <>''

    AND ORDER_NO=OH.ORDER_NO

    AND SEQ_NUM IN (

    SELECT MAX(SEQ_NUM)

    FROM PS_ORD_FRT_CHRG

    WHERE ORDER_NO=OH.ORDER_NO)) AS TRFT_SET_ID

    ,(

    SELECT SUM(OC1.FREIGHT_CHARGE)

    FROM PS_ORD_FRT_CHRG OC1

    WHERE OC1.ORDER_NO=OH.ORDER_NO) AS FREIGHT_CHARGE

    ,(

    SELECT FC.HM_FUEL_CHARGE_PCT

    FROM PS_HM_BI_FUEL_CHG FC

    WHERE FC.BUSINESS_UNIT = OH.BUSINESS_UNIT

    AND FC.CARRIER_ID = OH.CARRIER_ID

    AND EFFDT = (

    SELECT MAX(HM_F.EFFDT)

    FROM PS_HM_BI_FUEL_CHG HM_F

    WHERE HM_F.BUSINESS_UNIT = FC.BUSINESS_UNIT

    AND HM_F.CARRIER_ID = FC.CARRIER_ID

    AND HM_F.EFFDT <= OH.REQ_ARRIVAL_DTTM)) AS HM_FUEL_CHARGE_PCT

    ,(

    SELECT DISTINCT FREIGHT_PCT

    FROM PS_TRFT_FRT_BREAK GFP

    WHERE GFP.SETID = 'CORP1'

    AND GFP.TRFT_SET_ID =OC.TRFT_SET_ID

    AND GFP.DATE_BEGIN <= ID.SCHED_ARRV_DTTM

    AND GFP.DATE_END >= ID.SCHED_ARRV_DTTM

    AND GFP.EFF_STATUS = 'A') AS HM_FREIGHT_PCT

    ,(

    SELECT SN.TEXT254

    FROM PS_ORD_NOTE SN

    WHERE SN.BUSINESS_UNIT = 'HM001'

    AND SN.ORDER_NO = OH.ORDER_NO

    AND SN.NOTE_TYPE = 'SHIPNOTE') AS TEXT254

    ,(

    SELECT DISPLAY_PRICES

    FROM PS_CUST_OPTION CDPO

    , PS_SET_CNTRL_GROUP CDPO2

    WHERE CDPO2.REC_GROUP_ID = 'BI_01'

    AND CDPO.SETID = CDPO2.SETID

    AND CDPO2.SETCNTRLVALUE = 'HM001'

    AND CDPO.CUST_ID = OH.SOLD_TO_CUST_ID

    AND CDPO.EFFDT = (

    SELECT MAX(CDPO3.EFFDT)

    FROM PS_CUST_OPTION CDPO3

    WHERE CDPO3.SETID = CDPO.SETID

    AND CDPO3.CUST_ID = CDPO.CUST_ID

    AND CDPO3.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) ) AS DISPLAY_PRICES

    ,(

    SELECT DISTINCT MI.DESCR

    FROM PS_MASTER_ITEM_TBL MI

    WHERE MI.SETID='CORP1'

    AND INV_ITEM_GROUP IN('FIN_GOODS','FIN_GOODS_IMP')

    AND MI.INV_ITEM_ID =ID.INV_ITEM_ID

    AND MI.ITM_STATUS_EFFDT = (

    SELECT MAX(C_ED.ITM_STATUS_EFFDT)

    FROM PS_MASTER_ITEM_TBL C_ED

    WHERE MI.SETID = C_ED.SETID

    AND MI.INV_ITEM_ID =C_ED.INV_ITEM_ID

    AND C_ED.ITM_STATUS_EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))) AS DESCR

    ,(

    SELECT II.INV_ITEM_VOLUME

    FROM PS_INV_ITEMS II

    WHERE II.SETID='CORP1'

    AND II.INV_ITEM_ID=ID.INV_ITEM_ID

    AND II.EFFDT = (

    SELECT MAX(C_ED.EFFDT)

    FROM PS_INV_ITEMS C_ED

    WHERE II.SETID = C_ED.SETID

    AND II.INV_ITEM_ID = C_ED.INV_ITEM_ID

    AND C_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) ) AS VOLUME

    ,(

    SELECT PD.DSCNT_PCT

    FROM PS_PAY_TRMS_DSCNT PD

    WHERE SETID='CORP1'

    AND PD.EFFDT = (

    SELECT MAX(C_ED.EFFDT)

    FROM PS_PAY_TRMS_DSCNT C_ED

    WHERE PD.SETID = C_ED.SETID

    AND PD.PYMNT_TERMS_CD = C_ED.PYMNT_TERMS_CD

    AND C_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))

    AND PD.PYMNT_TERMS_CD=OH.PYMNT_TERMS_CD) AS PCT_VALUE

    FROM PS_ORD_HEADER OH, PS_ORD_LINE OL, PS_IN_DEMAND ID, PS_ORD_FRT_CHRG OC

    , (

    SELECT DISTINCT A.CUST_ID

    ,ID1.SHIP_CUST_NAME1

    ,A.ADDRESS1

    ,A.CITY

    ,A.STATE

    ,A.POSTAL

    ,A.PHONE

    ,ID1.ADDRESS_SEQ_NUM

    FROM PS_CUST_ADDRESS A

    ,PS_IN_DEMAND ID1

    WHERE A.SETID='CORP1'

    AND ID1.BUSINESS_UNIT='HM001'

    AND A.CUST_ID = ID1.SHIP_TO_CUST_ID

    AND A.ADDRESS_SEQ_NUM=ID1.ADDRESS_SEQ_NUM

    and A.EFFDT = (SELECT MAX(EFFDT) FROM PS_CUST_ADDRESS

    WHERE SETID = 'CORP1'

    AND CUST_ID = ID1.SHIP_TO_CUST_ID

    AND ADDRESS_SEQ_NUM = ID1.ADDRESS_SEQ_NUM

    AND EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))) AS SHIP_CUST

    , (

    SELECT DISTINCT A.CUST_ID

    ,B.NAME1

    ,A.ADDRESS1

    ,A.CITY

    ,A.STATE

    ,A.POSTAL

    ,A.PHONE

    FROM PS_CUST_ADDRESS A

    , PS_CUSTOMER B

    ,PS_ORD_HEADER OH

    --,PS_CUST_ADDR_SEQ SE

    WHERE A.SETID='CORP1'

    AND A.SETID=B.SETID

    AND OH.SOLD_TO_CUST_ID=A.CUST_ID

    AND A.CUST_ID=B.CUST_ID

    AND A.ADDRESS_SEQ_NUM=OH.ADDRESS_SEQ_NUM

    AND A.EFFDT = (

    SELECT MAX(B_ED.EFFDT)

    FROM PS_CUST_ADDRESS B_ED

    WHERE A.SETID = B_ED.SETID

    AND A.CUST_ID = B_ED.CUST_ID

    AND B_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) )AS BILL_CUST

    WHERE OL.BUSINESS_UNIT=OH.BUSINESS_UNIT

    AND ID.BUSINESS_UNIT= OH.BUSINESS_UNIT

    AND OC.BUSINESS_UNIT=ID.BUSINESS_UNIT

    AND OL.ORDER_NO=OH.ORDER_NO

    AND OH.ORDER_NO=ID.ORDER_NO

    AND OC.ORDER_NO=ID.ORDER_NO

    AND OL.ORDER_INT_LINE_NO=ID.ORDER_INT_LINE_NO

    AND OH.BILL_TO_CUST_ID=BILL_CUST.CUST_ID

    AND OH.SHIP_TO_CUST_ID=SHIP_CUST.CUST_ID

    AND OH.BUSINESS_UNIT='HM001'

  • If it's going to be used for reporting, I would consider to place this one into stored procedure where you could use temp tables for intermidiate values/sets.

    Your query doesn't look like a good candidate for making it view.

    Without DDL for tables, it's hard to see the best possible options, however, most likely replacing your inner queries with proper joins could bring better performance. Check this one:

    http://msdn.microsoft.com/en-us/library/aa213252(v=sql.80).aspx

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • twdavis-893252 (8/10/2011)


    Will you look at my SQL Statement and give me ideas on how to speed it up and make it leaner. It will be made into a view to report off of anf it will be used a lot....

    There are three steps to writing TSQL:

    Make it work - write something which meets the business need

    Make it fast - experiment with it using the EP as a guide

    Make it pretty - formatting and documentation.

    It's really hard to tell because it's horribly formatted, but I'd agree with Eugene - try putting those subqueries into the FROM list as proper joins. There's another one in the WHERE clause, do the same with that. Format the whole lot nicely so you can see at a glance what it's supposed to do, then start looking at the actual plan.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Are those DISTINCTs necessary, or are they just there for no good reason

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • As an example of Eugence's tip an excerpt of your query, but now using a left join (so you will still get an order listed even if its freight charge record is missing):

    SELECT OH.ORDER_NO

    ,OH.CUSTOMER_PO

    ,FREIGHT.CHARGE

    FROM PS_ORD_HEADER OH

    LEFT OUTER JOIN (

    SELECT OC1.ORDER_NO, SUM(OC1.FREIGHT_CHARGE) AS CHARGE

    FROM PS_ORD_FRT_CHRG OC1

    GROUP BY OC1.ORDER_NO

    ) FREIGHT ON FREIGHT.ORDER_NO = OH.ORDER_NO

    Next to Eugene's tip:

    Tip 2: You will want to remove the many distinct-s in the sub-queries and the main query. Instead do group by's on the smallest set of tables that may contain duplicates and only then join more tables to retrieve additional columns. Distinct requires sorting and then traversing to remove the duplicates; these are expensive operations. So you will want to avoid them as much as possible, and if you can't avoid them, always make sure the data set that is distinct-ed is as small as possible (both in row and column terms). Plus you best try to match the group by columns with one of the (primary) indexes on the table to group in.

    Tip 3: stripping the time component from a datetime value is better done using dateadd(day, datediff(day, 0, getdate()), 0) instead of SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10). Even worse, the latter returns a string whereas the column you're comparing to is most likely a datetime. This type mismatch can (and most likely will!) make the optimizer decide not to use any available indexes, even if these would match the query's needs.

    Tip 4: Use proper join syntax to indicate the relationships between your query's tables.

    Tip 5: Properly indent your code, especially if you intend put it onto a forum :-). It makes reading it so much easier. (you may use some free online tool like for example: http://poorsql.com/)



    Posting Data Etiquette - Jeff Moden[/url]
    Posting Performance Based Questions - Gail Shaw[/url]
    Hidden RBAR - Jeff Moden[/url]
    Cross Tabs and Pivots - Jeff Moden[/url]
    Catch-all queries - Gail Shaw[/url]


    If you don't have time to do it right, when will you have time to do it over?

  • Here's a formatted version. If you want us to help with this please post the actual execution plan. We can do a few things to help, but not a complete rewrite.

    SELECT DISTINCT

    OH.ORDER_NO

    , OH.CUSTOMER_PO

    , OH.PYMNT_TERMS_CD

    , OL.PRODUCT_ID

    , OL.PRICE_PROTECTED

    , SHIP_CUST.CUST_ID SHIP_TO_CUST_ID

    , BILL_CUST.CUST_ID BILL_TO_CUST_ID

    , SHIP_CUST.SHIP_CUST_NAME1 SHIP_TO_NAME

    , BILL_CUST.NAME1 BILL_TO_NAME

    , BILL_CUST.CITY BILL_TO_CITY

    , SHIP_CUST.CITY SHIP_TO_CITY

    , SHIP_CUST.ADDRESS1 SHIP_TO_ADDRESS1

    , BILL_CUST.ADDRESS1 BILL_TO_ADDRESS

    , SHIP_CUST.STATE SHIP_TO_STATE

    , BILL_CUST.STATE BILL_TO_STATE

    , SHIP_CUST.POSTAL SHIP_TO_POSTAL

    , BILL_CUST.POSTAL BILL_TO_POSTAL

    , SHIP_CUST.PHONE PHONE

    , BILL_CUST.PHONE PHONE2

    , ID.PARENT_PROD_ID

    , ID.DP_KIT_PARENT_QTY

    , ID.KIT_LINE_NBR

    , ID.QTY_PER

    , ID.INV_ITEM_ID

    , ID.QTY_REQUESTED

    , ID.QTY_BACKORDER

    , ID.QTY_SHIPPED

    , ID.LOAD_ID

    , ID.TMS_EXT_REF_ID

    , ID.ORDER_INT_LINE_NO

    , OH.SUPPORT_TEAM_CD

    , OH.CARRIER_ID

    , OH.OPRID_ENTERED_BY

    , OH.ORDER_DATE

    , OH.ORDER_GRP

    , OH.REQ_ARRIVAL_DTTM

    , (

    SELECT

    SUM(TAX_AMT)

    FROM

    PS_ORD_SCHEDULE OS1

    WHERE

    OS1.BUSINESS_UNIT = ID.BUSINESS_UNIT

    AND OS1.ORDER_NO = ID.ORDER_NO

    AND OS1.ORDER_INT_LINE_NO = ID.ORDER_INT_LINE_NO

    ) AS TAX_AMT

    , (

    SELECT DISTINCT

    ES.FRT_CHRG_OVERRIDE

    FROM

    PS_ORD_EST_SHIP ES

    WHERE

    ES.BUSINESS_UNIT = ID.BUSINESS_UNIT

    AND ES.ORDER_NO = ID.ORDER_NO

    AND ES.FRT_CHRG_OVERRIDE = 'Y'

    ) AS FRT_CHRG_OVERRIDE

    , OL.PRICE

    , (

    SELECT DISTINCT

    TRFT_SET_ID

    FROM

    PS_ORD_FRT_CHRG

    WHERE

    TRFT_SET_ID <> ''

    AND ORDER_NO = OH.ORDER_NO

    AND SEQ_NUM IN ( SELECT

    MAX(SEQ_NUM)

    FROM

    PS_ORD_FRT_CHRG

    WHERE

    ORDER_NO = OH.ORDER_NO )

    ) AS TRFT_SET_ID

    , (

    SELECT

    SUM(OC1.FREIGHT_CHARGE)

    FROM

    PS_ORD_FRT_CHRG OC1

    WHERE

    OC1.ORDER_NO = OH.ORDER_NO

    ) AS FREIGHT_CHARGE

    , (

    SELECT

    FC.HM_FUEL_CHARGE_PCT

    FROM

    PS_HM_BI_FUEL_CHG FC

    WHERE

    FC.BUSINESS_UNIT = OH.BUSINESS_UNIT

    AND FC.CARRIER_ID = OH.CARRIER_ID

    AND EFFDT = (

    SELECT

    MAX(HM_F.EFFDT)

    FROM

    PS_HM_BI_FUEL_CHG HM_F

    WHERE

    HM_F.BUSINESS_UNIT = FC.BUSINESS_UNIT

    AND HM_F.CARRIER_ID = FC.CARRIER_ID

    AND HM_F.EFFDT <= OH.REQ_ARRIVAL_DTTM

    )

    ) AS HM_FUEL_CHARGE_PCT

    , (

    SELECT DISTINCT

    FREIGHT_PCT

    FROM

    PS_TRFT_FRT_BREAK GFP

    WHERE

    GFP.SETID = 'CORP1'

    AND GFP.TRFT_SET_ID = OC.TRFT_SET_ID

    AND GFP.DATE_BEGIN <= ID.SCHED_ARRV_DTTM

    AND GFP.DATE_END >= ID.SCHED_ARRV_DTTM

    AND GFP.EFF_STATUS = 'A'

    ) AS HM_FREIGHT_PCT

    , (

    SELECT

    SN.TEXT254

    FROM

    PS_ORD_NOTE SN

    WHERE

    SN.BUSINESS_UNIT = 'HM001'

    AND SN.ORDER_NO = OH.ORDER_NO

    AND SN.NOTE_TYPE = 'SHIPNOTE'

    ) AS TEXT254

    , (

    SELECT

    DISPLAY_PRICES

    FROM

    PS_CUST_OPTION CDPO

    , PS_SET_CNTRL_GROUP CDPO2

    WHERE

    CDPO2.REC_GROUP_ID = 'BI_01'

    AND CDPO.SETID = CDPO2.SETID

    AND CDPO2.SETCNTRLVALUE = 'HM001'

    AND CDPO.CUST_ID = OH.SOLD_TO_CUST_ID

    AND CDPO.EFFDT = (

    SELECT

    MAX(CDPO3.EFFDT)

    FROM

    PS_CUST_OPTION CDPO3

    WHERE

    CDPO3.SETID = CDPO.SETID

    AND CDPO3.CUST_ID = CDPO.CUST_ID

    AND CDPO3.EFFDT <= SUBSTRING(CONVERT(CHAR , GETDATE() , 121) ,

    1 , 10)

    )

    ) AS DISPLAY_PRICES

    , (

    SELECT DISTINCT

    MI.DESCR

    FROM

    PS_MASTER_ITEM_TBL MI

    WHERE

    MI.SETID = 'CORP1'

    AND INV_ITEM_GROUP IN ( 'FIN_GOODS' , 'FIN_GOODS_IMP' )

    AND MI.INV_ITEM_ID = ID.INV_ITEM_ID

    AND MI.ITM_STATUS_EFFDT = (

    SELECT

    MAX(C_ED.ITM_STATUS_EFFDT)

    FROM

    PS_MASTER_ITEM_TBL C_ED

    WHERE

    MI.SETID = C_ED.SETID

    AND MI.INV_ITEM_ID = C_ED.INV_ITEM_ID

    AND C_ED.ITM_STATUS_EFFDT <= SUBSTRING(CONVERT(CHAR , GETDATE() , 121) , 1 , 10)

    )

    ) AS DESCR

    , (

    SELECT

    II.INV_ITEM_VOLUME

    FROM

    PS_INV_ITEMS II

    WHERE

    II.SETID = 'CORP1'

    AND II.INV_ITEM_ID = ID.INV_ITEM_ID

    AND II.EFFDT = (

    SELECT

    MAX(C_ED.EFFDT)

    FROM

    PS_INV_ITEMS C_ED

    WHERE

    II.SETID = C_ED.SETID

    AND II.INV_ITEM_ID = C_ED.INV_ITEM_ID

    AND C_ED.EFFDT <= SUBSTRING(CONVERT(CHAR , GETDATE() , 121) ,

    1 , 10)

    )

    ) AS VOLUME

    , (

    SELECT

    PD.DSCNT_PCT

    FROM

    PS_PAY_TRMS_DSCNT PD

    WHERE

    SETID = 'CORP1'

    AND PD.EFFDT = (

    SELECT

    MAX(C_ED.EFFDT)

    FROM

    PS_PAY_TRMS_DSCNT C_ED

    WHERE

    PD.SETID = C_ED.SETID

    AND PD.PYMNT_TERMS_CD = C_ED.PYMNT_TERMS_CD

    AND C_ED.EFFDT <= SUBSTRING(CONVERT(CHAR , GETDATE() , 121) ,

    1 , 10)

    )

    AND PD.PYMNT_TERMS_CD = OH.PYMNT_TERMS_CD

    ) AS PCT_VALUE

    FROM

    PS_ORD_HEADER OH

    , PS_ORD_LINE OL

    , PS_IN_DEMAND ID

    , PS_ORD_FRT_CHRG OC

    , (

    SELECT DISTINCT

    A.CUST_ID

    , ID1.SHIP_CUST_NAME1

    , A.ADDRESS1

    , A.CITY

    , A.STATE

    , A.POSTAL

    , A.PHONE

    , ID1.ADDRESS_SEQ_NUM

    FROM

    PS_CUST_ADDRESS A

    , PS_IN_DEMAND ID1

    WHERE

    A.SETID = 'CORP1'

    AND ID1.BUSINESS_UNIT = 'HM001'

    AND A.CUST_ID = ID1.SHIP_TO_CUST_ID

    AND A.ADDRESS_SEQ_NUM = ID1.ADDRESS_SEQ_NUM

    and A.EFFDT = (

    SELECT

    MAX(EFFDT)

    FROM

    PS_CUST_ADDRESS

    WHERE

    SETID = 'CORP1'

    AND CUST_ID = ID1.SHIP_TO_CUST_ID

    AND ADDRESS_SEQ_NUM = ID1.ADDRESS_SEQ_NUM

    AND EFFDT <= SUBSTRING(CONVERT(CHAR , GETDATE() , 121) ,

    1 , 10)

    )

    ) AS SHIP_CUST

    , (

    SELECT DISTINCT

    A.CUST_ID

    , B.NAME1

    , A.ADDRESS1

    , A.CITY

    , A.STATE

    , A.POSTAL

    , A.PHONE

    FROM

    PS_CUST_ADDRESS A

    , PS_CUSTOMER B

    , PS_ORD_HEADER OH

    --,PS_CUST_ADDR_SEQ SE

    WHERE

    A.SETID = 'CORP1'

    AND A.SETID = B.SETID

    AND OH.SOLD_TO_CUST_ID = A.CUST_ID

    AND A.CUST_ID = B.CUST_ID

    AND A.ADDRESS_SEQ_NUM = OH.ADDRESS_SEQ_NUM

    AND A.EFFDT = (

    SELECT

    MAX(B_ED.EFFDT)

    FROM

    PS_CUST_ADDRESS B_ED

    WHERE

    A.SETID = B_ED.SETID

    AND A.CUST_ID = B_ED.CUST_ID

    AND B_ED.EFFDT <= SUBSTRING(CONVERT(CHAR , GETDATE() , 121) ,

    1 , 10)

    )

    ) AS BILL_CUST

    WHERE

    OL.BUSINESS_UNIT = OH.BUSINESS_UNIT

    AND ID.BUSINESS_UNIT = OH.BUSINESS_UNIT

    AND OC.BUSINESS_UNIT = ID.BUSINESS_UNIT

    AND OL.ORDER_NO = OH.ORDER_NO

    AND OH.ORDER_NO = ID.ORDER_NO

    AND OC.ORDER_NO = ID.ORDER_NO

    AND OL.ORDER_INT_LINE_NO = ID.ORDER_INT_LINE_NO

    AND OH.BILL_TO_CUST_ID = BILL_CUST.CUST_ID

    AND OH.SHIP_TO_CUST_ID = SHIP_CUST.CUST_ID

    AND OH.BUSINESS_UNIT = 'HM001'

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply