[英]SELECT DISTINCT on two columns, with multiple columns returned
我正在使用以下查询
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
下面是输出
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.
我可以使用不同的 DBNAME 和 MSGNUM 获得输出吗
输出应如下所示
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.
您可以获得max
时间戳并使用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
尝试这个:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.