sql >> Databasteknik >  >> RDS >> Oracle

Hur använder man Alias ​​i Where-klausulen?

Du kan inte referera till kolumnaliaset i WHERE-satsen - dina alternativ är:

  • replikera CASE-satsen i WHERE-satsen
  • använd en underfråga:

    PROCEDURE P_LOAD_EXPIRED_ACCOUNT(pDayDiff NUMBER,
                                     ExpiredCur OUT MEGAGREEN_CUR)
    IS
    BEGIN
    
    
       OPEN ExpiredCur FOR
       SELECT x.account_name,
              x.service_type, 
              x.expired_date
         FROM (SELECT s.account_name,
                      s.service_type, 
                      CASE 
                         WHEN s.service_type = 1 THEN ADD_MONTHS(ACTIVATED_DATE,3)
                         WHEN s.service_type = 2 THEN ADD_MONTHS(ACTIVATED_DATE,6)
                         WHEN s.service_type = 3 THEN ADD_MONTHS(ACTIVATED_DATE,12)        
                      END AS EXPIRED_DATE
                 FROM SUBSCRIBERS s) x
        WHERE x.expired_date - CURRENT_DATE < pDayDiff;
    
    
    END;
    

    Oracle 9i+

    WITH summary AS (
      SELECT s.account_name,
             s.service_type, 
             CASE 
                WHEN s.service_type = 1 THEN ADD_MONTHS(ACTIVATED_DATE,3)
                WHEN s.service_type = 2 THEN ADD_MONTHS(ACTIVATED_DATE,6)
                WHEN s.service_type = 3 THEN ADD_MONTHS(ACTIVATED_DATE,12)        
             END AS EXPIRED_DATE
        FROM SUBSCRIBERS s)
       SELECT x.account_name,
              x.service_type, 
              x.expired_date
         FROM summary x
        WHERE x.expired_date - CURRENT_DATE < pDayDiff;
    



  1. Lagrad procedur och behörigheter - räcker det med EXECUTE?

  2. Nested Loop Join in Oracle 11g

  3. Prestandaskillnad:villkor placerat vid INNER JOIN vs WHERE-klausul

  4. Funktion för att ta bort accenter i postgreSQL