简体   繁体   English

有没有显示所需结果的Oracle查询,即按日期desc排序

[英]There is oracle query which is not showing the desired result i.e order by date desc

Oracle query that is used to select data from the tables.But it is not showing date descending. 用于从表中选择数据的Oracle查询,但未显示日期降序。

WITH cte AS
(
  SELECT DISTINCT cmoa.adate,
         subject,
         TYPE,
         to_char(cmoa.adate, 'DD/MM/YYYY') awarddate,
         (SELECT name
          FROM dopprod.cm_awards
          WHERE cm_awardsid = cmoa.awards) AS awardname
  FROM dopprod.cm_emp_master m
    INNER JOIN dopprod.cm_officer_awards cmoa ON m.cm_emp_masterid = cmoa.name
  WHERE m.cm_emp_masterid = '" + empcode + "'
)
SELECT row_number() OVER (ORDER BY awarddate) AS sno,
       subject,
       TYPE,
       awarddate,
       awardname
FROM dopprod.cte
ORDER BY sno

Why would it show dates in descending order, if you didn't tell it so? 如果您不这样说,为什么它会以降序显示日期? Shouldn't it be 不是吗

order by cmoa.adate DESC           --> your code is missing DESC

Try below - you need to specify desc in order by 请尝试以下操作-您需要按以下顺序指定desc

WITH cte
         AS (SELECT DISTINCT cmoa.adate,
                             subject,
                             TYPE,
                             TO_CHAR (cmoa.adate, 'DD/MM/YYYY') AwardDate,
                             (SELECT Name
                                FROM dopprod.cm_awards
                               WHERE cm_awardsid = cmoa.awards)
                                AS awardname
               FROM dopprod.CM_EMP_MASTER m
                    INNER JOIN dopprod.cm_officer_awards cmoa
                       ON m.cm_emp_masterid = cmoa.name
              WHERE m.CM_EMP_MASTERID = 'someid')
      SELECT ROW_NUMBER () OVER (ORDER BY cmoa.adate DESC) AS sno,
             subject,
             TYPE,
             AwardDate,
             awardname
        FROM dopprod.cte
    ORDER BY cmoa.adate desc

Why not just do this? 为什么不这样做呢?

SELECT rownum AS sno,
       subject, TYPE, awarddate, awardname
FROM dopprod.cte
ORDER BY adate DESC

If you want to sort by the date value, you need to include the date column, not the string column in the order by . 如果要按日期值排序,则需要按日期 order by包括日期列,而不是字符串列。

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

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