Adding INNER JOIN to Query With Subquery With Inli

2019-09-10 17:02发布

I have the following SQL which works:

Select TRADING_DWKEY,
       M_SM_USER_CODE,
       FIRST_TRADE,
       'New Footprint' Status,
       Null Q4Vol,
       CQVol 
  From (Select A.TRADING_DWKEY||A.RM_SM_USER_CODE,
               A.TRADING_DWKEY,
               A.RM_SM_USER_CODE,
               B.FIRST_TRADE, SUM(A.RISK_AMOUNT_ADJ)/1000000 CQVol
          from FACT_TRADE_ROLLUP A,
               (SELECT TRADING_DWKEY,
                       MIN(FIRST_TRADE_DATE) FIRST_TRADE 
                  FROM ACCOUNT_FIRST_LAST_TRADE_DATES
                  Having MIN(FIRST_TRADE_DATE) >= to_char(trunc((sysdate-1),'Y'),'dd-Mon-yy')
                  GROUP BY TRADING_DWKEY) B
          WHERE A.DATE_KEY>='01-Jan-16' AND
                ((A.PRODUCT_DWKEY IN ('RT') AND  A.Account_Type='Customer') OR
                  A.PRODUCT_DWKEY IN('OB','BS','MM')) AND
                A.Role = 'SM' AND
                A.TRADING_DWKEY = B.TRADING_DWKEY
          GROUP BY A.TRADING_DWKEY||A.RM_SM_USER_CODE,
                   A.TRADING_DWKEY,
                   A.RM_SM_USER_CODE,
                   B.FIRST_TRADE
          HAVING SUM(A.RISK_AMOUNT_ADJ)>=20000000) NewFP;

When I attempt to add a INNER JOIN to the subquery (to pull a name instead of a code), I get an error that A.RM_SM_USER_CODE is not a valid identifier on the line where I write the INNER JOIN. However, I am positive this field exists in the FACT_TRADE_ROLLUP table. I suspect the error has something to do with where I place the INNER JOIN, but cannot figure it.

Here is the fully modified SQL (but I get the error even if I just place the INNER JOIN statement in without actually modifying the columns.

Select TRADING_DWKEY,
       RM_SM_NAME,
       FIRST_TRADE,
       'New Footprint' Status,
       Null Q4Vol,
       CQVol 
  From (Select A.TRADING_DWKEY||A.RM_SM_USER_CODE,
               A.TRADING_DWKEY,
               q.RM_SM_NAME,
               B.FIRST_TRADE,
               SUM(A.RISK_AMOUNT_ADJ)/1000000 CQVol 
          from FACT_TRADE_ROLLUP A,
               (SELECT TRADING_DWKEY, MIN(FIRST_TRADE_DATE) FIRST_TRADE  
                  FROM ACCOUNT_FIRST_LAST_TRADE_DATES
                  Having MIN(FIRST_TRADE_DATE)>=to_char(trunc((sysdate-1),'Y'),'dd-Mon-yy')
                  GROUP BY TRADING_DWKEY) B
  --- error thrown on this line, specifying it at the end
          INNER JOIN REF_RM_SM_REGION q
            on q.RM_SM_USER_CODE = A.RM_SM_USER_CODE
          WHERE A.DATE_KEY>='01-Jan-16' AND
                ((A.PRODUCT_DWKEY IN('RT') AND A.Account_Type='Customer') OR 
                  A.PRODUCT_DWKEY IN('OB','BS','MM')) AND
                A.Role = 'SM' AND
                A.TRADING_DWKEY = B.TRADING_DWKEY
          GROUP BY A.TRADING_DWKEY||A.RM_SM_USER_CODE,
                   A.TRADING_DWKEY,
                   q.RM_SM_NAME,
                   B.FIRST_TRADE
          HAVING SUM(A.RISK_AMOUNT_ADJ)>=20000000) NewFP;

2条回答
手持菜刀,她持情操
2楼-- · 2019-09-10 17:21
Select TRADING_DWKEY, RM_SM_NAME, FIRST_TRADE, 'New Footprint' Status, Null Q4Vol, CQVol 
From (
    Select
        A.TRADING_DWKEY || A.RM_SM_USER_CODE, /* No name. So is this even used? */
        A.TRADING_DWKEY,
        q.RM_SM_NAME, B.FIRST_TRADE, SUM(A.RISK_AMOUNT_ADJ) / 1000000 CQVol 
    from FACT_TRADE_ROLLUP A INNER JOIN /* <--- Changed here... */
        (
        SELECT TRADING_DWKEY, MIN(FIRST_TRADE_DATE) FIRST_TRADE  
        FROM ACCOUNT_FIRST_LAST_TRADE_DATES
        Having MIN(FIRST_TRADE_DATE)>=to_char(trunc((sysdate-1),'Y'),'dd-Mon-yy')
        GROUP BY TRADING_DWKEY /* Very odd it ran with this after HAVING */
        ) B
            ON B.TRADING_DWKEY = A.TRADING_DWKEY /* <--- ...and here */
        INNER JOIN REF_RM_SM_REGION q on q.RM_SM_USER_CODE = A.RM_SM_USER_CODE
    WHERE
        A.DATE_KEY>='01-Jan-16'
            AND (A.PRODUCT_DWKEY IN('RT') AND A.Account_Type='Customer')
                OR A.PRODUCT_DWKEY IN('OB','BS','MM'))
            AND A.Role = 'SM'
  /* removed line that was here */ 
  GROUP BY
      A.TRADING_DWKEY||A.RM_SM_USER_CODE, A.TRADING_DWKEY,
      q.RM_SM_NAME, B.FIRST_TRADE
  HAVING SUM(A.RISK_AMOUNT_ADJ) > =20000000
) NewFP;

I made a minimal effort to clean it up. Hopefully I didn't break it in the process. As noted it was probably the mixing of two types of join syntax that caused the error.

查看更多
等我变得足够好
3楼-- · 2019-09-10 17:27

Without any DDL statements defining the tables it is difficult to test but:

  1. You are comparing dates using strings - Oracle is probably doing an implicit conversion back to a date using the value of the NLS_DATE_FORMAT session parameter (which is hopefully DD-MON-YY) but if this ever changes then it will break the query (and the query won't have changed so it will be a pain to debug). Stop using strings as dates and use date literals - i.e. DATE '2016-01-01'.
  2. You have the first HAVING clause before the GROUP BY clause - swap them around.
  3. You also are mixing a legacy comma join and ANSI join - replace the comma join with an INNER JOIN and move the join condition out of the WHERE clause.
  4. Grouping by A.TRADING_DWKEY||A.RM_SM_USER_CODE is inefficient. You already have A.TRADING_DWKEY as one of the GROUP BY terms so don't concatenate the strings just include the other column in the GROUP BY clause.

Something like:

Select TRADING_DWKEY,
       RM_SM_NAME,
       FIRST_TRADE,
       'New Footprint' Status,
       Null Q4Vol,
       CQVol 
From (
  Select A.TRADING_DWKEY,
         q.RM_SM_NAME,
         B.FIRST_TRADE,
         SUM(A.RISK_AMOUNT_ADJ)/1000000 CQVol 
  from   FACT_TRADE_ROLLUP A
         INNER JOIN
         ( SELECT   TRADING_DWKEY,
                    MIN(FIRST_TRADE_DATE) FIRST_TRADE  
           FROM     ACCOUNT_FIRST_LAST_TRADE_DATES
           GROUP BY TRADING_DWKEY
           HAVING   MIN(FIRST_TRADE_DATE)>= trunc(sysdate-1,'Y')
         ) B
         ON ( A.TRADING_DWKEY=B.TRADING_DWKEY )
         INNER JOIN REF_RM_SM_REGION q
         ON ( q.RM_SM_USER_CODE = A.RM_SM_USER_CODE )
  WHERE A.DATE_KEY >= DATE '2016-01-01'
  AND   ( (    A.PRODUCT_DWKEY IN('RT')
           AND A.Account_Type='Customer'
          )
        OR A.PRODUCT_DWKEY IN('OB','BS','MM')
        )
  AND   A.Role = 'SM' 
  GROUP BY A.RM_SM_USER_CODE,
           A.TRADING_DWKEY,
           q.RM_SM_NAME,
           B.FIRST_TRADE
  HAVING SUM(A.RISK_AMOUNT_ADJ)>=20000000
);
查看更多
登录 后发表回答