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
  • 9,282
  • 5
  • 36
  • 53

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
  • 16,830
  • 14
  • 78
  • 122
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
  • 129,880
  • 21
  • 220
  • 259