弱弱的问下,1000行的SQL算长的吗

弱弱的问下,1000行的SQL算长的吗

WITH
    t_payinfo (pi_ord_id, pi_amount, pi_ppn_txnid,pi_ppn_payer_email,pi_ppn_paystatus, pi_pft_pnref
    , pi_pft_ccnum, pi_pft_cctype, pi_pft_avs_resp, pi_ofp_refnum) AS
    (
        SELECT
            ord_id AS pi_ord_id,
            SUM(COALESCE(ppn_amount,0))+SUM(COALESCE(b.pft_amount,0))+SUM(COALESCE(c.ofp_amount,0))
                                                 AS pi_amount,
            MAX(ppn_txnid)                       AS pi_ppn_txnid,
            REPLACE(MIN(ppn_payer_email),' ','') AS pi_ppn_payer_email,
            MIN(ppn_paystatus)                   AS pi_ppn_paystatus,
            (
                SELECT
                    a.pft_pnref
                FROM
                    ei.payflowtxn a
                WHERE
                    a.pft_id=MAX(b.pft_id)
            ) AS pi_pft_pnref,
            (
                SELECT
                    a.pft_ccnum_enc
                FROM
                    ei.payflowtxn a
                WHERE
                    a.pft_id=MAX(b.pft_id)
            ) AS pi_pft_ccnum,
            (
                SELECT
                    a.pft_cctype
                FROM
                    ei.payflowtxn a
                WHERE
                    a.pft_id=MAX(b.pft_id)
            ) AS pi_pft_cctype,
            CASE ord_paymethod
                WHEN 'CC'
                THEN
                    (
                        SELECT
                            a.pft_avs_resp
                        FROM
                            ei.payflowtxn a
                        WHERE
                            a.pft_id=MAX(b.pft_id)
                    )
                WHEN 'PP'
                THEN
                    (
                        SELECT
                            CASE ppn_b.ppn_address_status
                                WHEN 'confirmed'
                                THEN '&AVSADDR=Y&AVSZIP=Y&IAVS=N'
                                ELSE '&AVSADDR=N&AVSZIP=N&IAVS=N'
                            END
                        FROM
                            ei.paypalnotification ppn_b
                        WHERE
                            ppn_b.ppn_txnid=MIN(ppn_a.ppn_txnid)
                    )
                ELSE NULL
            END               AS pi_pft_avs_resp,
            MAX(c.ofp_refnum) AS pi_ofp_refnum
        FROM
            ei.orders
        LEFT JOIN ei.paypalnotification ppn_a
        ON
            (
                ppn_ord_id=ord_id
            AND ord_paymethod='PP'
            AND ppn_processstatus='processed_notif'
            AND ppn_paystatus='Completed'
            AND ppn_amount>0
            )
        LEFT JOIN ei.payflowtxn b
        ON
            (
                b.pft_ord_id=ord_id
            AND ord_paymethod='CC'
            AND b.pft_result='0'
            AND b.pft_txntype IN ('CAPONLY')
            )
        LEFT JOIN ei.offlinepayment c
        ON
            (
                c.ofp_ord_id = ord_id
            AND c.ofp_paystatus = 'Verified'
            AND c.ofp_amount > 0
            AND
                (
                    ppn_a.ppn_amount <= 0
                 OR ppn_a.ppn_amount IS NULL
                )
            AND
                (
                    b.pft_amount <= 0
                 OR b.pft_amount IS NULL
                )
            )
        WHERE
            ord_ctl_nbr=#{catId}
        AND
            (
                ord_ordtype = 'B'
             OR
                (
                    ord_ordtype = 'O'
                AND ord_bord_id IS NULL
                )
            )
        AND ord_fraud_state IN ('R','AR')
        AND ord_fraud_updts #{W1} #{W2} #{W3}
        GROUP BY
            ord_id,
            ord_paymethod
    )
    ,
    t_all AS
    (
        SELECT
            t1.*,
            DECIMAL( COALESCE(
                CASE ori_id
                    WHEN
                        (
                            SELECT
                                MAX(ori_id)
                            FROM
                                ei.orderitem
                            WHERE
                                ori_ord_id=ord_id
                        )
                    THEN prod_discount - COALESCE(
                        (
                            SELECT
                                SUM(DECIMAL(prod_discount * ori_bidamt/NULLIF(ord_prodamt,0),12,2)
                                )
                            FROM
                                ei.orderitem
                            WHERE
                                ori_ord_id=ord_id
                            AND ori_id<>
                                (
                                    SELECT
                                        MAX(ori_id)
                                    FROM
                                        ei.orderitem
                                    WHERE
                                        ori_ord_id=ord_id
                                )
                        )
                        ,0)
                    ELSE DECIMAL(prod_discount * ori_bidamt/NULLIF(ord_prodamt,0),12,2)
                END ,0) ,12,2) AS discountAmtTemp
        FROM
            (
                SELECT
                    0          AS bundleStorePriceTotal,
                    ori_bidamt AS bundlePrice,
                    ord_id,
                    shp_id,
                    ori_id,
                    ord_chargedts,
                    ori_bidamt,
                    'SHP-'||CHAR(shp_id)   AS shipmentID,
                    CHAR(ord_id)           AS orderNum,
                    '99'||CHAR(ord_vec_id) AS custID,
                    CHAR(DATE(ord_chargedts),USA)||' ' ||LEFT(CHAR(TIME(ord_chargedts),USA),5) ||
                    RIGHT(CHAR(TIME(ord_chargedts),JIS),3) ||RIGHT(CHAR(TIME(ord_chargedts),USA),3)
                                                                    AS orderDate,
                    ''                                              AS pO,
                    shm_name                                        AS shipVia,
                    ''                                              AS FOB,
                    ''                                              AS term,
                    ''                                              AS reqDate,
                    pi_pft_ccnum                                    AS ccNum,
                    ''                                              AS ccDate,
                    ''                                              AS pin,
                    RTRIM(shipto.add_fname)||' '|| shipto.add_lname AS shipToName,
                    shipto.add_company_name                         AS shipToCompany,
                    shipto.add_address1                             AS shipToAddress1,
                    shipto.add_address2                             AS shipToAddress2,
                    shipto.add_city                                 AS shipToCity,
                    shipto.add_state                                AS shipToState,
                    shipto.add_zip                                  AS shipToZip,
                    shipto.add_country                              AS shipToCountry,
                    CASE
                        WHEN
                            (
                                shipto.add_phone IS NULL
                             OR shipto.add_phone = ''
                            )
                        THEN '8008918595'
                        ELSE REPLACE(REPLACE(REPLACE(shipto.add_phone,')',''),'(',''),'-','')
                    END                                             AS shipToPhone,
                    REPLACE(shipto.add_email,' ','')                AS shipToEmail,
                    RTRIM(billto.add_fname)||' '|| billto.add_lname AS billToName,
                    billto.add_company_name                         AS billToCompany,
                    billto.add_address1                             AS billToAddress1,
                    billto.add_address2                             AS billToAddress2,
                    billto.add_city                                 AS billToCity,
                    billto.add_state                                AS billToState,
                    billto.add_zip                                  AS billTozip,
                    billto.add_country                              AS billToCountry,
                    CASE
                        WHEN
                            (
                                billto.add_phone IS NULL
                             OR billto.add_phone = ''
                            )
                        THEN '8008918595'
                        ELSE REPLACE(REPLACE(REPLACE(billto.add_phone,')',''),'(',''),'-','')
                    END                              AS billToPhone,
                    REPLACE(billto.add_email,' ','') AS billToEmail,
                    ord_total                        AS orderTotal,
                    ori_bidamt                       AS unitPrice,
                    ord_prodamt + ord_delta_prodamt  AS prodTotal,
                    ord_shipamt+ord_delta_shipamt-COALESCE(
                    (
                        SELECT
                            SUM(ABS(ODST_DISC_AMT))
                        FROM
                            ei.orderdiscount
                        WHERE
                            ODST_ORD_ID = ord_id
                        AND ODST_TYPE=1
                    )
                    ,0) AS freight,
                    COALESCE(
                    (
                        SELECT
                            OSC_CHARGE1
                        FROM
                            ei.ordersurcharge
                        WHERE
                            OSC_ORD_ID = ord_id
                    )
                    ,0) AS surcharge1,
                    1   AS orderQty,
                    CASE
                        WHEN ori_csn_id IS NULL
                        THEN csku_name
                        ELSE
                            (
                                SELECT
                                    csn_serialnumber
                                FROM
                                    ei.cskuserialnumbers
                                WHERE
                                    ori_csn_id=csn_id
                                AND csn_csku_id=csku_id
                                AND csn_ctl_nbr=#{catId}
                                FETCH
                                    FIRST ROW ONLY
                            )
                    END       AS part,
                    ''        AS description,
                    ord_state AS status,
                    ''        AS freightAcct,
                    ''        AS notes,
                    ''        AS approval,
                    CASE vns_ven_id
                        WHEN 1
                        THEN
                            (
                                SELECT
                                    CASE
                                        WHEN esld_auctiontype IN (0,2)
                                        THEN 'eBayAuction'
                                        WHEN esld_auctiontype IN (7,9,10)
                                        THEN 'eBayFixed'
                                        ELSE ''
                                    END
                                FROM
                                    ei.ebayskulaunchdetails,
                                    ei.launchedsku,
                                    ei.bidhistory
                                WHERE
                                    esld_sld_id=lds_sld_id
                                AND lds_id=bih_lds_id
                                AND bih_id=ori_bih_id
                            )
                        WHEN 2
                        THEN 'Catalog Store'
                        WHEN 3
                        THEN 'edeal Auctions'
                        ELSE ''
                    END AS venueType,
                    COALESCE(
                    (
                        SELECT
                            lds_remoteauckey
                        FROM
                            ei.launchedsku,
                            ei.bidhistory
                        WHERE
                            lds_id=bih_lds_id
                        AND bih_id=ori_bih_id
                    )
                    ,CHAR(si_id)) AS listing,
                    (
                        SELECT
                            CASE TRANSLATE(REPLACE(ccfv_field5,' ',''),'','0123456789.')
                                WHEN ''
                                THEN REPLACE(ccfv_field5,' ','')
                                ELSE NULL
                            END
                        FROM
                            ei.cskucustomfieldvalues
                        WHERE
                            ccfv_csku_id=csku_id
                    )             AS screenSize,
                    vec_name      AS userName,
                    vec_regip     AS remoteIP,
                    ord_paymethod AS paymentMethod,
                    ORD_TAX_PST + (ORD_DELTA_TAXAMT - DECIMAL(COALESCE(ROUND(ORD_DELTA_TAXAMT *
                    ORD_TAX_COUNTY/NULLIF(ORD_TAXAMT,0),2),0),12,2) - DECIMAL(COALESCE(ROUND(
                    ORD_DELTA_TAXAMT * ORD_TAX_CITY/NULLIF(ORD_TAXAMT,0),2),0),12,2)) AS
                    orderTaxState,
                    ORD_TAX_COUNTY + DECIMAL(COALESCE(ROUND(ORD_DELTA_TAXAMT * ORD_TAX_COUNTY/
                    NULLIF(ORD_TAXAMT,0),2),0),12,2) AS orderTaxCounty,
                    ORD_TAX_CITY + DECIMAL(COALESCE(ROUND(ORD_DELTA_TAXAMT * ORD_TAX_CITY/NULLIF(
                    ORD_TAXAMT,0),2),0),12,2)     AS orderTaxCity,
                    ord_taxamt + ord_delta_taxamt AS orderTax,
                    CASE vns_ven_id
                        WHEN 7
                        THEN ord_total
                        ELSE pi_amount
                    END                                AS payAmount,
                    pi_ppn_paystatus                   AS ppPayStatus,
                    REPLACE(pi_ppn_payer_email,' ','') AS ppUserID,
                    CASE
                        WHEN ord_paymethod = 'PP'
                        THEN pi_ppn_txnid
                        WHEN ord_paymethod = 'CC'
                        AND pi_pft_pnref IS NOT NULL
                        THEN pi_pft_pnref
                        ELSE pi_ofp_refnum
                    END           AS transactionID,
                    pi_pft_cctype AS ccType,
                    CASE vns_ven_id
                        WHEN 1
                        THEN
                            (
                                SELECT
                                    CASE esld_auctiontype
                                        WHEN 7
                                        THEN 'F'
                                        WHEN 9
                                        THEN 'F'
                                        ELSE
                                            CASE esld_buyitnowprice
                                                WHEN ori_bidamt
                                                THEN 'F'
                                                ELSE NULL
                                            END
                                    END
                                FROM
                                    ei.ebayskulaunchdetails,
                                    ei.launchedsku,
                                    ei.bidhistory
                                WHERE
                                    esld_sld_id=lds_sld_id
                                AND lds_id=bih_lds_id
                                AND bih_id=ori_bih_id
                            )
                        WHEN 2
                        THEN 'F'
                        WHEN 3
                        THEN
                            (
                                SELECT
                                    CASE edsld_format
                                        WHEN 'F'
                                        THEN 'F'
                                        ELSE NULL
                                    END
                                FROM
                                    ei.edealskulaunchdetails,
                                    ei.launchedsku,
                                    ei.bidhistory
                                WHERE
                                    edsld_sld_id=lds_sld_id
                                AND lds_id=bih_lds_id
                                AND bih_id=ori_bih_id
                            )
                        WHEN 6
                        THEN
                            (
                                SELECT
                                    CASE osld_buyitnowprice
                                        WHEN ori_bidamt
                                        THEN 'F'
                                        ELSE NULL
                                    END
                                FROM
                                    ei.overstockskulaunchdetails,
                                    ei.launchedsku,
                                    ei.bidhistory
                                WHERE
                                    osld_sld_id=lds_sld_id
                                AND lds_id=bih_lds_id
                                AND bih_id=ori_bih_id
                            )
                        WHEN 7
                        THEN
                            (
                                SELECT
                                    CASE ubsld_buyitnowprice
                                        WHEN ori_bidamt
                                        THEN 'F'
                                        ELSE NULL
                                    END
                                FROM
                                    ei.ubidskulaunchdetails,
                                    ei.launchedsku,
                                    ei.bidhistory
                                WHERE
                                    ubsld_sld_id=lds_sld_id
                                AND lds_id=bih_lds_id
                                AND bih_id=ori_bih_id
                            )
                    END           AS listingType,
                    ord_promocode AS discountPromoCode,
                    ABS(ord_discount) - COALESCE(
                    (
                        SELECT
                            SUM(ABS(ODST_DISC_AMT))
                        FROM
                            ei.orderdiscount
                        WHERE
                            ODST_ORD_ID = ord_id
                        AND ODST_TYPE=1
                    )
                    ,0) AS prod_discount,
                    COALESCE(
                    (
                        SELECT
                            SUM(ori_bidamt)
                        FROM
                            ei.orderitem
                        WHERE
                            ori_ord_id=ord_id
                    )
                    ,0)               AS ord_prodamt,
                    ABS(ord_discount) AS orderDiscount,
                    (
                        SELECT
                            ubo_remote_ord_id
                        FROM
                            ei.ubidorder
                        WHERE
                            ubo_ord_id=ord_id
                    ) AS ubidOrderID,
                    CASE COALESCE(POSSTR(NULLIF(pi_pft_avs_resp,''),'AVSADDR='),-1)
                        WHEN 0
                        THEN 'E'
                        WHEN -1
                        THEN NULL
                        ELSE SUBSTR(pi_pft_avs_resp,POSSTR(pi_pft_avs_resp,'AVSADDR=')+8,1)
                    END AS avsAddress,
                    CASE COALESCE(POSSTR(NULLIF(pi_pft_avs_resp,''),'AVSZIP='),-1)
                        WHEN 0
                        THEN 'E'
                        WHEN -1
                        THEN NULL
                        ELSE SUBSTR(pi_pft_avs_resp,POSSTR(pi_pft_avs_resp,'AVSZIP=')+7,1)
                    END avsZip,
                    CASE COALESCE(POSSTR(NULLIF(pi_pft_avs_resp,''),'IAVS='),-1)
                        WHEN 0
                        THEN 'E'
                        WHEN -1
                        THEN NULL
                        ELSE SUBSTR(pi_pft_avs_resp,POSSTR(pi_pft_avs_resp,'IAVS=')+5,1)
                    END avsCountry,
                    CASE COALESCE(ord_prodamt,0)
                        WHEN 0
                        THEN 0
                        ELSE COALESCE(DECIMAL((ori_bidamt*(1-(DOUBLE(ord_discount-COALESCE(
                            (
                                SELECT
                                    SUM(ABS(ODST_DISC_AMT))
                                FROM
                                    ei.orderdiscount
                                WHERE
                                    ODST_ORD_ID = ord_id
                                AND ODST_TYPE=1
                            )
                            ,0))/ord_prodamt))), 12,2),0)
                    END AS DiscountSalePrice,
                    COALESCE(
                    (
                        SELECT
                            cv_value
                        FROM
                            ei.categoryattribute,
                            ei.cattvalues
                        WHERE
                            catt_ctl_nbr=#{catId}
                        AND cv_catt_id=catt_id
                        AND cv_csku_id=csku_id
                        AND catt_name ='wirelessspec'
                        AND cv_lang='def'
                    )
                    ,'') AS WirelessNIC,
                    COALESCE(
                    (
                        SELECT
                            cv_value
                        FROM
                            ei.categoryattribute,
                            ei.cattvalues
                        WHERE
                            catt_ctl_nbr=#{catId}
                        AND cv_catt_id=catt_id
                        AND cv_csku_id=csku_id
                        AND catt_name ='bundled_sku'
                        AND cv_lang='def'
                    )
                    ,'') AS Bundled_SKU
                FROM
                    t_payinfo,
                    ei.orderitem,
                    ei.storeitem,
                    ei.orders,
                    ei.venuecustaddress shipto_vca,
                    ei.venuecustaddress billto_vca,
                    ei.addressdetails shipto,
                    ei.addressdetails billto,
                    ei.ordershipment,
                    ei.shipmethod,
                    ei.vencustomer,
                    ei.venuesites,
                    ei.venue,
                    ei.catalogsku
                LEFT JOIN ei.cskucustomfieldvalues
                ON
                    ccfv_csku_id = csku_id
                WHERE
                    ori_ord_id=ord_id
                AND ori_csku_id=csku_id
                AND si_csku_id=csku_id
                AND ord_vec_id=vec_id
                AND ord_vca_id=shipto_vca.vca_id
                AND shipto_vca.vca_add_nbr = shipto.add_nbr
                AND ord_billto_vca_id=billto_vca.vca_id
                AND billto_vca.vca_add_nbr = billto.add_nbr
                AND shp_id=ori_shp_id
                AND ori_vns_id=vns_id
                AND vns_ven_id=ven_id
                AND shp_shm_id=shm_id
                AND pi_ord_id=ord_id
                AND csku_bundle = 'N'
            ) AS t1
        UNION
        SELECT
            t2.*,
            DECIMAL(bundlePrice/ord_prodamt*unitPrice/bundleStorePriceTotal*prod_discount,12,2) AS
            discountAmtTemp
        FROM
            (
                SELECT
                    (
                        SELECT
                            SUM(si_price)
                        FROM
                            ei.orderitem,
                            ei.storeitem
                        WHERE
                            ori_sbd_ori_id =ori.ori_sbd_ori_id
                        AND ori_csku_id=si_csku_id
                    )                     AS bundleStorePriceTotal,
                    bundle_ori.ori_bidamt AS bundlePrice,
                    ord_id,
                    shp_id,
                    ori.ori_id,
                    ord_chargedts,
                    ori.ori_bidamt,
                    'SHP-'||CHAR(shp_id)   AS shipmentID,
                    CHAR(ord_id)           AS orderNum,
                    '99'||CHAR(ord_vec_id) AS custID,
                    CHAR(DATE(ord_chargedts),USA)||' ' ||LEFT(CHAR(TIME(ord_chargedts),USA),5) ||
                    RIGHT(CHAR (TIME(ord_chargedts),JIS),3) ||RIGHT(CHAR(TIME(ord_chargedts),USA),3
                    )                                               AS orderDate,
                    ''                                              AS pO,
                    shm_name                                        AS shipVia,
                    ''                                              AS FOB,
                    ''                                              AS term,
                    ''                                              AS reqDate,
                    pi_pft_ccnum                                    AS ccNum,
                    ''                                              AS ccDate,
                    ''                                              AS pin,
                    RTRIM(shipto.add_fname)||' '|| shipto.add_lname AS shipToName,
                    shipto.add_company_name                         AS shipToCompany,
                    shipto.add_address1                             AS shipToAddress1,
                    shipto.add_address2                             AS shipToAddress2,
                    shipto.add_city                                 AS shipToCity,
                    shipto.add_state                                AS shipToState,
                    shipto.add_zip                                  AS shipToZip,
                    shipto.add_country                              AS shipToCountry,
                    CASE
                        WHEN
                            (
                                shipto.add_phone IS NULL
                             OR shipto.add_phone = ''
                            )
                        THEN '8008918595'
                        ELSE REPLACE(REPLACE(REPLACE(shipto.add_phone,')',''),'(',''),'-','')
                    END                                             AS shipToPhone,
                    REPLACE(shipto.add_email,' ','')                AS shipToEmail,
                    RTRIM(billto.add_fname)||' '|| billto.add_lname AS billToName,
                    billto.add_company_name                         AS billToCompany,
                    billto.add_address1                             AS billToAddress1,
                    billto.add_address2                             AS billToAddress2,
                    billto.add_city                                 AS billToCity,
                    billto.add_state                                AS billToState,
                    billto.add_zip                                  AS billTozip,
                    billto.add_country                              AS billToCountry,
                    CASE
                        WHEN
                            (
                                billto.add_phone IS NULL
                             OR billto.add_phone = ''
                            )
                        THEN '8008918595'
                        ELSE REPLACE(REPLACE(REPLACE(billto.add_phone,')',''),'(',''),'-','')
                    END                              AS billToPhone,
                    REPLACE(billto.add_email,' ','') AS billToEmail,
                    ord_total                        AS orderTotal,
                    si_price                         AS unitPrice,
                    ord_prodamt + ord_delta_prodamt  AS prodTotal,
                    ord_shipamt+ord_delta_shipamt-COALESCE(
                    (
                        SELECT
                            SUM(ABS(ODST_DISC_AMT))
                        FROM
                            ei.orderdiscount
                        WHERE
                            ODST_ORD_ID = ord_id
                        AND ODST_TYPE=1
                    )
                    ,0) AS freight,
                    COALESCE(
                    (
                        SELECT
                            OSC_CHARGE1
                        FROM
                            ei.ordersurcharge
                        WHERE
                            OSC_ORD_ID = ord_id
                    )
                    ,0) AS surcharge1,
                    1   AS orderQty,
                    CASE
                        WHEN ori.ori_csn_id IS NULL
                        THEN csku.csku_name
                        ELSE
                            (
                                SELECT
                                    csn_serialnumber
                                FROM
                                    ei.cskuserialnumbers
                                WHERE
                                    ori.ori_csn_id=csn_id
                                AND csn_csku_id=csku.csku_id
                                AND csn_ctl_nbr=#{catId}
                                FETCH
                                    FIRST ROW ONLY
                            )
                    END       AS part,
                    ''        AS description,
                    ord_state AS status,
                    ''        AS freightAcct,
                    ''        AS notes,
                    ''        AS approval,
                    CASE vns_ven_id
                        WHEN 1
                        THEN
                            (
                                SELECT
                                    CASE
                                        WHEN esld_auctiontype IN (0,2)
                                        THEN 'eBayAuction'
                                        WHEN esld_auctiontype IN (7,9,10)
                                        THEN 'eBayFixed'
                                        ELSE ''
                                    END
                                FROM
                                    ei.ebayskulaunchdetails,
                                    ei.launchedsku,
                                    ei.bidhistory
                                WHERE
                                    esld_sld_id=lds_sld_id
                                AND lds_id=bih_lds_id
                                AND bih_id=bundle_ori.ori_bih_id
                            )
                        WHEN 2
                        THEN 'Catalog Store'
                        WHEN 3
                        THEN 'edeal Auctions'
                        ELSE ''
                    END AS venueType,
                    COALESCE(
                    (
                        SELECT
                            lds_remoteauckey
                        FROM
                            ei.launchedsku,
                            ei.bidhistory
                        WHERE
                            lds_id=bih_lds_id
                        AND bih_id=ori.ori_bih_id
                    )
                    ,CHAR(si_id)) AS listing,
                    (
                        SELECT
                            CASE TRANSLATE(REPLACE(ccfv_field5,' ',''),'','0123456789.')
                                WHEN ''
                                THEN REPLACE(ccfv_field5,' ','')
                                ELSE NULL
                            END
                        FROM
                            ei.cskucustomfieldvalues
                        WHERE
                            ccfv_csku_id=csku.csku_id
                    )             AS screenSize,
                    vec_name      AS userName,
                    vec_regip     AS remoteIP,
                    ord_paymethod AS paymentMethod,
                    ORD_TAX_PST + (ORD_DELTA_TAXAMT - DECIMAL(COALESCE(ROUND(ORD_DELTA_TAXAMT *
                    ORD_TAX_COUNTY/NULLIF(ORD_TAXAMT,0),2),0),12,2) - DECIMAL(COALESCE(ROUND(
                    ORD_DELTA_TAXAMT * ORD_TAX_CITY/NULLIF(ORD_TAXAMT,0),2),0),12,2)) AS
                    orderTaxState,
                    ORD_TAX_COUNTY + DECIMAL(COALESCE(ROUND(ORD_DELTA_TAXAMT * ORD_TAX_COUNTY/
                    NULLIF( ORD_TAXAMT,0),2),0),12,2) AS orderTaxCounty,
                    ORD_TAX_CITY + DECIMAL(COALESCE(ROUND(ORD_DELTA_TAXAMT * ORD_TAX_CITY/NULLIF(
                    ORD_TAXAMT ,0),2),0),12,2)    AS orderTaxCity,
                    ord_taxamt + ord_delta_taxamt AS orderTax,
                    CASE vns_ven_id
                        WHEN 7
                        THEN ord_total
                        ELSE pi_amount
                    END                                AS payAmount,
                    pi_ppn_paystatus                   AS ppPayStatus,
                    REPLACE(pi_ppn_payer_email,' ','') AS ppUserID,
                    CASE
                        WHEN ord_paymethod = 'PP'
                        THEN pi_ppn_txnid
                        WHEN ord_paymethod = 'CC'
                        AND pi_pft_pnref IS NOT NULL
                        THEN pi_pft_pnref
                        ELSE pi_ofp_refnum
                    END           AS transactionID,
                    pi_pft_cctype AS ccType,
                    CASE vns_ven_id
                        WHEN 1
                        THEN
                            (
                                SELECT
                                    CASE esld_auctiontype
                                        WHEN 7
                                        THEN 'F'
                                        WHEN 9
                                        THEN 'F'
                                        ELSE
                                            CASE esld_buyitnowprice
                                                WHEN ori.ori_bidamt
                                                THEN 'F'
                                                ELSE NULL
                                            END
                                    END
                                FROM
                                    ei.ebayskulaunchdetails,
                                    ei.launchedsku,
                                    ei.bidhistory
                                WHERE
                                    esld_sld_id=lds_sld_id
                                AND lds_id=bih_lds_id
                                AND bih_id=ori.ori_bih_id
                            )
                        WHEN 2
                        THEN 'F'
                        WHEN 3
                        THEN
                            (
                                SELECT
                                    CASE edsld_format
                                        WHEN 'F'
                                        THEN 'F'
                                        ELSE NULL
                                    END
                                FROM
                                    ei.edealskulaunchdetails,
                                    ei.launchedsku,
                                    ei.bidhistory
                                WHERE
                                    edsld_sld_id=lds_sld_id
                                AND lds_id=bih_lds_id
                                AND bih_id=ori.ori_bih_id
                            )
                        WHEN 6
                        THEN
                            (
                                SELECT
                                    CASE osld_buyitnowprice
                                        WHEN ori.ori_bidamt
                                        THEN 'F'
                                        ELSE NULL
                                    END
                                FROM
                                    ei.overstockskulaunchdetails,
                                    ei.launchedsku,
                                    ei.bidhistory
                                WHERE
                                    osld_sld_id=lds_sld_id
                                AND lds_id=bih_lds_id
                                AND bih_id=ori.ori_bih_id
                            )
                        WHEN 7
                        THEN
                            (
                                SELECT
                                    CASE ubsld_buyitnowprice
                                        WHEN ori.ori_bidamt
                                        THEN 'F'
                                        ELSE NULL
                                    END
                                FROM
                                    ei.ubidskulaunchdetails,
                                    ei.launchedsku,
                                    ei.bidhistory
                                WHERE
                                    ubsld_sld_id=lds_sld_id
                                AND lds_id=bih_lds_id
                                AND bih_id=ori.ori_bih_id
                            )
                    END           AS listingType,
                    ord_promocode AS discountPromoCode,
                    ABS(ord_discount) - COALESCE(
                    (
                        SELECT
                            SUM(ABS(ODST_DISC_AMT))
                        FROM
                            ei.orderdiscount
                        WHERE
                            ODST_ORD_ID = ord_id
                        AND ODST_TYPE=1
                    )
                    ,0) AS prod_discount,
                    COALESCE(
                    (
                        SELECT
                            SUM(prod_ori.ori_bidamt)
                        FROM
                            ei.orderitem AS prod_ori
                        WHERE
                            prod_ori.ori_ord_id=ord_id
                    )
                    ,0)               AS ord_prodamt,
                    ABS(ord_discount) AS orderDiscount,
                    (
                        SELECT
                            ubo_remote_ord_id
                        FROM
                            ei.ubidorder
                        WHERE
                            ubo_ord_id=ord_id
                    ) AS ubidOrderID,
                    CASE COALESCE(POSSTR(NULLIF(pi_pft_avs_resp,''),'AVSADDR='),-1)
                        WHEN 0
                        THEN 'E'
                        WHEN -1
                        THEN NULL
                        ELSE SUBSTR(pi_pft_avs_resp,POSSTR(pi_pft_avs_resp,'AVSADDR=')+8,1)
                    END AS avsAddress,
                    CASE COALESCE(POSSTR(NULLIF(pi_pft_avs_resp,''),'AVSZIP='),-1)
                        WHEN 0
                        THEN 'E'
                        WHEN -1
                        THEN NULL
                        ELSE SUBSTR(pi_pft_avs_resp,POSSTR(pi_pft_avs_resp,'AVSZIP=')+7,1)
                    END avsZip,
                    CASE COALESCE(POSSTR(NULLIF(pi_pft_avs_resp,''),'IAVS='),-1)
                        WHEN 0
                        THEN 'E'
                        WHEN -1
                        THEN NULL
                        ELSE SUBSTR(pi_pft_avs_resp,POSSTR(pi_pft_avs_resp,'IAVS=')+5,1)
                    END avsCountry,
                    COALESCE(DECIMAL(( (si_price- DOUBLE(ord_discount-COALESCE(
                    (
                        SELECT
                            SUM(ABS(ODST_DISC_AMT))
                        FROM
                            ei.orderdiscount
                        WHERE
                            ODST_ORD_ID = ord_id
                        AND ODST_TYPE=1
                    )
                    ,0)) *
                    (
                        SELECT
                            ori_bidamt
                        FROM
                            ei.orderitem
                        WHERE
                            ori_id =ori.ori_sbd_ori_id
                    )
                    /ord_prodamt *si_price /
                    (
                        SELECT
                            SUM(si_price)
                        FROM
                            ei.orderitem,
                            ei.storeitem
                        WHERE
                            ori_sbd_ori_id =ori.ori_sbd_ori_id
                        AND ori_csku_id=si_csku_id
                    )
                    )), 12,2),0) AS DiscountSalePrice,
                    COALESCE(
                    (
                        SELECT
                            cv_value
                        FROM
                            ei.categoryattribute,
                            ei.cattvalues
                        WHERE
                            catt_ctl_nbr=#{catId}
                        AND cv_catt_id=catt_id
                        AND cv_csku_id=csku.csku_id
                        AND catt_name ='wirelessspec'
                        AND cv_lang='def'
                    )
                    ,'') AS WirelessNIC,
                    COALESCE(
                    (
                        SELECT
                            cv_value
                        FROM
                            ei.categoryattribute,
                            ei.cattvalues
                        WHERE
                            catt_ctl_nbr=#{catId}
                        AND cv_catt_id=catt_id
                        AND cv_csku_id=csku.csku_id
                        AND catt_name ='bundled_sku'
                        AND cv_lang='def'
                    )
                    ,'') AS Bundled_SKU
                FROM
                    t_payinfo,
                    ei.orderitem AS bundle_ori,
                    ei.orderitem AS ori,
                    ei.storeitem,
                    ei.orders,
                    ei.venuecustaddress shipto_vca,
                    ei.venuecustaddress billto_vca,
                    ei.addressdetails shipto,
                    ei.addressdetails billto,
                    ei.ordershipment,
                    ei.shipmethod,
                    ei.vencustomer,
                    ei.venuesites,
                    ei.venue,
                    ei.catalogsku AS bundle_csku,
                    ei.catalogsku AS csku
                LEFT JOIN ei.cskucustomfieldvalues
                ON
                    ccfv_csku_id = csku.csku_id
                WHERE
                    ord_ctl_nbr = #{catId}
                AND csku.csku_ctl_nbr = #{catId}
                AND pi_ord_id = ord_id
                AND bundle_ori.ori_ord_id = ord_id
                AND ori.ori_sbd_ori_id = bundle_ori.ori_id
                AND ori.ori_csku_id = csku.csku_id
                AND bundle_ori.ori_csku_id = bundle_csku.csku_id
                AND bundle_csku.csku_bundle = 'Y'
                AND shp_id= bundle_ori.ori_shp_id
                AND shp_shm_id=shm_id
                AND ord_vca_id=shipto_vca.vca_id
                AND shipto_vca.vca_add_nbr = shipto.add_nbr
                AND ord_billto_vca_id=billto_vca.vca_id
                AND billto_vca.vca_add_nbr = billto.add_nbr
                AND ori.ori_vns_id=vns_id
                AND vns_ven_id=ven_id
                AND si_csku_id=csku.csku_id
                AND ord_vec_id=vec_id
            ) AS t2
        ORDER BY
            ord_id,
            shp_id,
            ori_id,
            ord_chargedts
    )
SELECT
    t_all.*,
    DECIMAL( COALESCE(
        CASE ori_id
            WHEN
                (
                    SELECT
                        MAX(a.ori_id)
                    FROM
                        t_all a
                    WHERE
                        a.ord_id = t_all.ord_id
                )
            THEN prod_discount-
                (
                    SELECT
                        COALESCE(SUM(discountAmtTemp), 0)
                    FROM
                        t_all c
                    WHERE
                        c.ord_id= t_all.ord_id
                    AND c.ori_id<>
                        (
                            SELECT
                                MAX(a.ori_id)
                            FROM
                                t_all a
                            WHERE
                                a.ord_id = t_all.ord_id
                        )
                )
            ELSE discountAmtTemp
        END ,0) ,12,2) AS discountAmount
FROM
    t_all;


你可能感兴趣的:(JOIN,sql,c,Date,null,email)