简体   繁体   中英

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.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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