简体   繁体   中英

Order by date desc and find the id which are not in descending

Here is my data

ID   VER_ID DUE_DATE
1478  1.0   28-MAR-16
1352  1.0   8-MAY-17
1479  2.0   28-MAR-17
1481  2.0   9-June-17

I need to identify all the rows- IDs which are not in descending order, when i sort by due_date desc and group by ver_id.

With my above data Expected result: (Because the ver 2.0 has the ID aligned with date - Descending)

ID   VER_ID DUE_DATE
1478  1.0   28-MAR-16
1352  1.0   8-MAY-17

Hmmm. One definition of "not in descending order" is that the value is either greater than the previous value or less than the next one.

With that definition:

select t.*
from (select t.*,
             lag(due_date) over (order by id) as prev_due_date,
             lead(due_date) over (order by id) as next_due_date,
      from t
     ) t
where prev_due_date < due_date or next_due_date > due_date;

You can find it out by using "LAG" analytical function. It will give you the "ID" column value of preceding row and you can compare it with "ID" column of current row.

For example- In below emp table, records are ordered by "hiredate" desc and case statement is checking for "empno" which is less then the previous employee's "empno". So, for the first column "FLAG" in the table if EMPNO is not descending it gives "0" else "1".

  SELECT (CASE
            WHEN empno > lag (empno) OVER (ORDER BY hiredate desc) THEN 0
            ELSE 1
         END) as flag, a.*
    FROM emp a
ORDER BY hiredate  DESC;

Table: EMP

FLAG    |   EMPNO       |   ENAME       |   JOB         |   MGR         |   HIREDATE        |   SAL         |   COMM    |   DEPTNO
--------+---------------+---------------+---------------+---------------+-------------------+---------------+-----------+---------
1       |   7876        |   ADAMS       |   CLERK       |   7788        |   12-Jan-83       |   1100        |           |   20
1       |   7788        |   SCOTT       |   ANALYST     |   7566        |   9-Dec-82        |   3000        |           |   20
0       |   7934        |   MILLER      |   CLERK       |   7782        |   23-Jan-82       |   1300        |           |   10
1       |   7902        |   FORD        |   ANALYST     |   7566        |   3-Dec-81        |   3000        |           |   20
1       |   7900        |   JAMES       |   CLERK       |   7698        |   3-Dec-81        |   950         |           |   30
1       |   7839        |   KING        |   PRESIDENT   |               |   17-Nov-81       |   5000        |           |   10
1       |   7654        |   MARTIN      |   SALESMAN    |   7698        |   28-Sep-81       |   1250        |   1400    |   30
0       |   7844        |   TURNER      |   SALESMAN    |   7698        |   8-Sep-81        |   1500        |   0       |   30
1       |   7782        |   CLARK       |   MANAGER     |   7839        |   9-Jun-81        |   2450        |           |   10
1       |   7698        |   BLAKE       |   MANAGER     |   7839        |   1-May-81        |   2850        |           |   30
1       |   7566        |   JONES       |   MANAGER     |   7839        |   2-Apr-81        |   2975        |           |   20
1       |   7521        |   WARD        |   SALESMAN    |   7698        |   22-Feb-81       |   1250        |   500     |   30
1       |   7499        |   ALLEN       |   SALESMAN    |   7698        |   20-Feb-81       |   1600        |   300     |   30
1       |   7369        |   SMITH       |   CLERK       |   7902        |   17-Dec-80       |   800         |           |   20

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