Quantcast
Channel: Teradata Forums - All forums
Viewing all articles
Browse latest Browse all 27759

Why subqueries in case are not working in teradata? - response (9) by dnoeth

$
0
0

Hi Ashish,
this query is actually running in Oracle? 
I don't think any other DBMS allows Scalar Subqueries based on a Dervied Table/Inline View.
 
But it should be easy to rewrite :-)
Move "a" into a WITH clause (Oracle supports it, too) should work:

WITH a (asset_wid,PR_VIS_ORG_WID,SELL_DT_WID,prod_wid,contact_wid,accnt_wid) AS
   (
   SELECT
      W_ASSET_D.ROW_WID           asset_wid
    , W_ASSET_F.PR_VIS_ORG_WID    PR_VIS_ORG_WID
    , W_ASSET_F.SELL_DT_WID       SELL_DT_WID
    , MIN(WC_PRODUCT_DH.PROD_WID) prod_wid
    , W_ASSET_F.contact_wid       contact_wid
    , w_asset_f.Accnt_wid         accnt_wid
   FROM
      W_ASSET_D
    , W_ASSET_F
    , W_PRODUCT_DH
    , W_INT_ORG_D
    ,WC_PRODUCT_DH
    ,w_product_d
   WHERE
      W_ASSET_D.ROW_WID                 = W_ASSET_F.ASSET_WID
      AND W_ASSET_F.PROD_WID            = W_PRODUCT_DH.PROD_WID
      AND W_INT_ORG_D.ROW_WID           = W_ASSET_F.PR_VIS_ORG_WID
      AND WC_PRODUCT_DH.LVL8ANC_PROD_ID = W_PRODUCT_DH.LVL8ANC_PROD_ID
      AND w_product_d.row_wid           = W_PRODUCT_DH.prod_wid
      AND w_product_d.x_BU_UNIT IN ('TMPC','TM')
      AND W_INT_ORG_D.BU_NAME = 'TMPC'
   GROUP BY
      W_ASSET_D.ROW_WID
    ,W_ASSET_D.LICENSE_NO
    ,W_ASSET_F.PR_VIS_ORG_WID
    ,W_ASSET_F.contact_wid
    ,w_asset_f.Accnt_wid
    , W_ASSET_F.SELL_DT_WID
   )
SELECT DISTINCT
   a.asset_wid
 ,a.PR_VIS_ORG_WID
 ,a.SELL_DT_WID
 ,a.prod_wid
 ,a.contact_wid
 ,a.accnt_wid
 , CASE
      WHEN (
            a.contact_wid                         <> 0
            AND W_PARTY_PER_D.x_district IS NOT NULL
         )
      THEN (CASE
            WHEN W_PARTY_PER_D.X_DISTRICT IN
               (
               SELECT
                  UPPER(WC_ORG_EXT_XM_F.attrib_04)
               FROM
                  wc_org_ext_xm_f
               WHERE
                  pf_org_wid = a.PR_VIS_ORG_WID
               )
            THEN 0
            ELSE 1
         END )
      ELSE (CASE
            WHEN W_PARTY_ORG_D.X_DISTRICT IN
               (
               SELECT
                  UPPER(WC_ORG_EXT_XM_F.attrib_04)
               FROM
                  wc_org_ext_xm_f
               WHERE
                  pf_org_wid = a.PR_VIS_ORG_WID
               )
            THEN 0
            ELSE 1
         END )
   
   END infringement
FROM
   a
 , WC_ORG_EXT_XM_F
 , W_PARTY_PER_D
 , W_PARTY_ORG_D
WHERE
   WC_ORG_EXT_XM_F.PF_ORG_WID              = a.PR_VIS_ORG_WID
   AND WC_ORG_EXT_XM_F.prod_wid            = a.prod_wid
   AND a.contact_wid                       = W_PARTY_PER_D.row_wid
   AND a.accnt_wid                         = W_PARTY_ORG_D.row_wid
   AND a.sell_dt_wid                       = TO_NUMBER(TO_CHAR(SYSDATE,'yyyymmdd'))-1
   AND WC_ORG_EXT_XM_F.attrib_04 IS NOT NULL

 
I don't know about the business logic but it seems you might also fully remove the Scalar Subqueries with some OLAP functions, which should result in better performance (in both TD and Oracle)
 
Dieter


Viewing all articles
Browse latest Browse all 27759

Trending Articles



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