简体   繁体   English

在子查询中按日期排序?

[英]ORDER BY date in sub-query?

I'm using Oracle 11g, come across the situation to order the data based on sub-query. 我正在使用Oracle 11g,遇到这种情况要根据子查询对数据进行排序。

Assume having the two tables STATUS_TABLE and TXN_TABLE . 假设有两个表STATUS_TABLETXN_TABLE I want to short the TXN_TABLE based on the STATUS_TABLE 's DATE_INS field. 我想短TXN_TABLE基础上, STATUS_TABLEDATE_INS场。

Below is the sample query, 下面是示例查询,

SELECT ID
FROM STATUS_TABLE
WHERE STATUS = 'Pass'
ORDER BY DATE_INS DESC

This query returns me a list of ID's in descending order based on the DATE_INS. 该查询基于DATE_INS以降序返回ID的列表。 So the above query has been extended little bit for TXN_TABLE as follows 因此,上述查询已针对TXN_TABLE进行了一些扩展,如下所示

SELECT *
FROM TXN_TABLE
WHERE ID IN (SELECT ID FROM STATUS_TABLE
             WHERE STATUS = 'Pass'
             ORDER BY DATE_INS DESC)

which gives me: 这给了我:

ORA-00907: missing right parenthesis ORA-00907:缺少右括号

I gone through the document, they where telling that it is not possible to make a order class at sub-query. 我浏览了该文档,他们告诉我们不可能在子查询中创建订单类。 so is there any workaround for this scenario to make it work? 因此,此方案是否有任何解决方法可以使其正常工作?

Comment on below, if you need any more details on this. 如果您需要更多详细信息,请在下面评论。 Any help would be appreciated! 任何帮助,将不胜感激!

Maybe you can try this 也许你可以试试看

SELECT tmp.* FROM
    (SELECT tx.*, st.DATE_INS AS DATE_INS_ST
    FROM TXN_TABLE tx, STATUS_TABLE st
    WHERE tx.ID = st.ID AND st.STATUS = 'Pass') tmp
ORDER BY DATE_INS_ST DESC

Ordering the subquery will give no ordering on the final result; 对子查询进行排序不会对最终结果进行排序; you could rewrite your query as: 您可以将查询重写为:

SELECT *
FROM TXN_TABLE
    INNER JOIN STATUS_TABLE
USING(ID)
WHERE STATUS='Pass'
ORDER BY DATE_INS DESC

assuming, as rightly oberved by jarlh, that your ID columns can not have duplicate values 就像jarlh正确认为的那样,假设您的ID列不能有重复的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM