简体   繁体   中英

SELECT DISTINCT on two columns, with multiple columns returned

I am using the below query

SELECT TIMESTAMP,substr(DBNAME,1,10) as DBNAME, MSGSEVERITY as SEV, MSGNUM, substr(MSG,1,80) as MSG_trunc FROM TABLE ( PD_GET_LOG_MSGS( CURRENT_TIMESTAMP - 30 DAYS)) AS T WHERE MSGSEVERITY NOT IN 'I'  ORDER BY TIMESTAMP

Below is the output

TIMESTAMP                  DBNAME     SEV MSGNUM      MSG_TRUNC
-------------------------- ---------- --- ----------- ---------------------------------------------------
2019-11-17-05.45.33.266795 ABC        W          1602 ADM1602W  Rollforward recovery has been initiated.

2019-11-17-05.45.39.086945 XYZ        W          1602 ADM1602W  Rollforward recovery has been initiated.

2019-11-17-05.45.42.960668 ABC        W          1602 ADM1602W  Rollforward recovery has been initiated.

2019-11-17-05.45.48.287948 XYZ        W          1602 ADM1602W  Rollforward recovery has been initiated.

2019-11-17-07.04.09.038759 ABC        W          1611 ADM1611W  The rollforward recovery phase has been completed.

2019-11-17-07.04.14.688757 XYZ        W          1611 ADM1611W  The rollforward recovery phase has been completed.

2019-11-17-07.04.18.339282 XYZ        W          1611 ADM1611W  The rollforward recovery phase has been completed.

Can I get the output with distinct DBNAME and MSGNUM

The output should be as below

TIMESTAMP                  DBNAME     SEV MSGNUM      MSG_TRUNC
-------------------------- ---------- --- ----------- ---------------------------------------------------
2019-11-17-05.45.33.266795 ABC        W          1602 ADM1602W  Rollforward recovery has been initiated.

2019-11-17-05.45.39.086945 XYZ        W          1602 ADM1602W  Rollforward recovery has been initiated.

2019-11-17-07.04.09.038759 ABC        W          1611 ADM1611W  The rollforward recovery phase has been completed.

2019-11-17-07.04.14.688757 XYZ        W          1611 ADM1611W  The rollforward recovery phase has been completed.

You can get the max timestamp and use group by .

SELECT max(TIMESTAMP)
    , DBNAME
    , SEV
    , MSGNUM
    , MSG_trunc 
FROM    
    (SELECT TIMESTAMP
        , substr(DBNAME,1,10) as DBNAME
        , MSGSEVERITY as SEV
        , MSGNUM
        , substr(MSG,1,80) as MSG_trunc 
    FROM TABLE (PD_GET_LOG_MSGS(CURRENT_TIMESTAMP - 30 DAYS)) AS T 
    WHERE MSGSEVERITY NOT IN 'I') t  
GROUP BY DBNAME
    , SEV
    , MSGNUM
    , MSG_trunc 
ORDER BY TIMESTAMP

Try this:

SELECT *
FROM
(
SELECT 
  TIMESTAMP
, substr(DBNAME, 1, 10) as DBNAME
, MSGSEVERITY as SEV
, MSGNUM
, substr(MSG, 1, 80) as MSG_trunc 
, ROWNUMBER() OVER (PARTITION BY DBNAME, MSGNUM ORDER BY TIMESTAMP) RN
FROM TABLE (PD_GET_LOG_MSGS(CURRENT_TIMESTAMP - 30 DAYS)) AS T 
WHERE MSGSEVERITY NOT IN 'I'
)
WHERE RN=1
ORDER BY TIMESTAMP;

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