简体   繁体   中英

Select distinct id with max date according to department

I have a table such as following

RevNo | RevContent          | PIC      | Created
-------------------------------------------------------
 00   | Testing Purpose     | Smith   | 2008-01-11
 01   | Testing Purpose     | Windsor | 2008-02-01
 02   | Test                | Thorn   | 2008-01-05
 02   | Testing             | Baker   | 2008-03-01
 03   | Testing only        | Sykes   | 2008-01-20

I want the output like below, it display the latest date for each rev no. and other details

RevNo | RevContent          | PIC      | Created
----------------------------------------------------
 00   | Testing Purpose     | Windsor | 2008-02-11
 01   | Testing             | Baker   | 2008-03-01
 02   | Testing only        | Sykes   | 2008-01-20

But when I run the sql, it display the value with the latest date only like below, I want it display the latest date for each rev no. and it based on department(session).

RevNo | RevContent          | PIC      | Created
----------------------------------------------------
 01   | Testing             | Baker   | 2008-03-01

My query:

SELECT CCSMASTERLISTREVNO, CCSREVCONTENT, CCSPREPAREDREV, CCSREVEFFECTIVEDATE
FROM CCS2_TBL_MASTERLIST a 
WHERE CCSEQUIPMENTDPMT = :DPMT AND CCSREVEFFECTIVEDATE = 
 (
SELECT MAX(CCSREVEFFECTIVEDATE) FROM CCS2_TBL_MASTERLIST 
 GROUP BY CCSMASTERLISTREVNO HAVING CCSMASTERLISTREVNO =a.CCSMASTERLISTREVNO
  ) 
ORDER BY CCSMASTERLISTREVNO DESC

Your subquery group by each department (CCSMASTERLISTREVNO) but the HAVING clause is wrong. Instead you should use compound columns in an IN clause. Something like this:

SELECT CCSMASTERLISTREVNO
       , CCSREVCONTENT
       , CCSPREPAREDREV
       , CCSREVEFFECTIVEDATE
FROM CCS2_TBL_MASTERLIST a 
WHERE CCSEQUIPMENTDPMT = :DPMT 
AND (CCSMASTERLISTREVNO, CCSREVEFFECTIVEDATE) in 
 (
     SELECT CCSMASTERLISTREVNO, MAX(CCSREVEFFECTIVEDATE) 
     FROM CCS2_TBL_MASTERLIST 
     GROUP BY CCSMASTERLISTREVNO )
  ) 
ORDER BY CCSMASTERLISTREVNO DESC 

One option is to use row_number()

SELECT RevNo
    ,RevContent
    ,PIC
    ,Created
FROM (
    SELECT t.*
        ,row_number() OVER (
            PARTITION BY RevNo ORDER BY Created DESC
            ) AS rn
    FROM t
    )
WHERE rn = 1;

Another is to use the LAST aggregate function.

SELECT RevNo
    ,MAX(RevContent) KEEP ( DENSE_RANK LAST ORDER BY CREATED ) as RevContent
    ,MAX(PIC) KEEP ( DENSE_RANK LAST ORDER BY CREATED ) as PIC
    ,MAX(Created) as CREATED
FROM t
GROUP BY RevNo;

I didn't really understand your question but maybe this can help you.

SELECT RevNo, RevContent, PIC, (Created) as Created
FROM your_table_name 
GROUP BY RevNo
ORDER BY Created DESC

I think you need first Group your entries by the RevNo and then you sort by taking the Last Created of each group.

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