Domanda

I'm getting an odd error, that I think I might know the answer to, but still can't seem to fix. It is saying that "LatestUpdatedDate" is an invalid identifier and I think it is because it runs the ORDER BY statement before it gets to the CASE statement, but that doesn't really make much sense with the way it is structured. Have any ideas?

SELECT * FROM 
( 
  SELECT sym.*, 
    (
    CASE WHEN shr_last_updt_dt >= trd_last_updt_dt 
    THEN shr_last_updt_dt 
    ELSE trd_last_updt_dt END
    ) AS LatestUpdatedDate,
    row_number() over ( ORDER BY LatestUpdatedDate DESC ) AS line_number 
  FROM trdg_sym sym  
  WHERE ((wkly_rpt_dt = :settleDate)) AND ((lower(rpt_type_cd)=lower(:reportType)))
)  
WHERE line_number BETWEEN 1 AND 25 ORDER BY line_number

enter image description here

È stato utile?

Soluzione

The aliases will given after generating result set so you can't use them in Windowing functions. You can do this:

SELECT * FROM 
( 
  SELECT sym.*, 
    (
    CASE WHEN shr_last_updt_dt >= trd_last_updt_dt 
    THEN shr_last_updt_dt 
    ELSE trd_last_updt_dt END
    ) AS LatestUpdatedDate,
    row_number() over ( ORDER BY 
     (
        CASE WHEN shr_last_updt_dt >= trd_last_updt_dt 
        THEN shr_last_updt_dt 
        ELSE trd_last_updt_dt END
     ) DESC ) AS line_number 
  FROM trdg_sym sym  
  WHERE ((wkly_rpt_dt = :settleDate)) AND ((lower(rpt_type_cd)=lower(:reportType)))
)  
WHERE line_number BETWEEN 1 AND 25 ORDER BY line_number

or wrap your subquery to another

SELECT * FROM 
(
  SELECT *, 
    row_number() over ( ORDER BY LatestUpdatedDate DESC ) AS line_number 
  FROM
  (
     SELECT sym.*, 
       (
          CASE WHEN shr_last_updt_dt >= trd_last_updt_dt 
          THEN shr_last_updt_dt 
          ELSE trd_last_updt_dt END
        ) AS LatestUpdatedDate 
     FROM trdg_sym sym  
     WHERE ((wkly_rpt_dt = :settleDate)) AND ((lower(rpt_type_cd)=lower(:reportType)))
   )
)  
WHERE line_number BETWEEN 1 AND 25 ORDER BY line_number

P.S. I don't sure for oacle syntax, but should you give aliases to subquery?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top