使用DESC和ROWNUM进行SELECT

时间:2011-07-04 12:48:19

标签: sql oracle

我当前的查询:

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;

但是因为在我获得无效结果之前在订单之前应用了rownum。我在stackoverflow上找到了以下主题:How do I limit the number of rows returned by an Oracle query after ordering?但讨论了select,而不是select into

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:3)

同样的方法适用于您引用的问题:

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