2

My current query:

SELECT DACTIONMILLIS, DACTIONDATE INTO WF_DACTIONMILLIS, WF_DACTIONDATE 
FROM WORKFLOWHISTORY 
WHERE ddocname=? and LOWER(DACTION)=?
and lower(DWFSTEPNAME)=?
and lower(DUSER)=? 
and rownum = 1
ORDER BY DACTIONDATE desc;

But because the rownum is applied before the order by I'm getting invalid results. I found the following topic on stackoverflow: How do I limit the number of rows returned by an Oracle query after ordering? but that discusses a select, not a select into

Community
  • 1
  • 1
Peeter
  • 8,952
  • 4
  • 34
  • 52

2 Answers2

5
SELECT DACTIONMILLIS, DACTIONDATE INTO WF_DACTIONMILLIS, WF_DACTIONDATE 
FROM (
    SELECT DACTIONMILLIS, DACTIONDATE, WF_DACTIONDATE 
    FROM WORKFLOWHISTORY 
    WHERE ddocname=? and LOWER(DACTION)=?
    and lower(DWFSTEPNAME)=?
    and lower(DUSER)=? 
    ORDER BY DACTIONDATE desc
)
WHERE rownum = 1
Cyril Gandon
  • 15,798
  • 12
  • 69
  • 116
3

The same approach applies as in the question you referenced:

SELECT DACTIONMILLIS, DACTIONDATE
INTO WF_DACTIONMILLIS, WF_DACTIONDATE 
from
( SELECT DACTIONMILLIS, DACTIONDATE
  FROM WORKFLOWHISTORY 
  WHERE ddocname=? and LOWER(DACTION)=?
  and lower(DWFSTEPNAME)=?
  and lower(DUSER)=? 
  ORDER BY DACTIONDATE desc
)
WHERE rownum = 1
Tony Andrews
  • 121,972
  • 20
  • 211
  • 249