获取仅按 DB2/400 中的用户分组的众多结果中的最新结果
Getting latest of many results only grouped by user in DB2/400
我正在发布我的查询和结果,但我试图将其限制为仅包含创建者的最新状态和时间戳。
select t2.creator, t1.event_id, t2.status, max(t2.createdTS)
from schema.event T1
inner join schema.event_to_event_statust T2
on t1.event_id = t2.event_id
group by t2.creator, t1.event_id, t2.status;
creator | event_id | status | createdTS
-----------------------------------------------------
123 1 New 2019-01-29 17:27:50.899408
123 1 Live 2019-01-29 17:29:50.899408
123 1 Closed 2019-01-29 17:45:50.899408
456 5 New 2019-01-29 17:32:50.899408
456 5 Live 2019-01-29 17:48:50.899408
我想要的这组结果实际上是
creator | event_id | status | createdTS
-----------------------------------------------------
123 1 Closed 2019-01-29 17:45:50.899408
456 5 Live 2019-01-29 17:48:50.899408
所以每个创作者我应该只有一行,它应该是最新的记录。我做错了什么?
尝试
with temp as (
select t2.creator, t1.event_id, t2.status, t2.createdTS
row_number() over (partition by t2.creator order by t2.createdTS desc) as rownum
from schema.event T1
inner join schema.event_to_event_statust T2
on t1.event_id = t2.event_id
group by t2.creator, t1.event_id, t2.status
)
select creator, event_id, status, createdTS
from temp
where rownum = 1
@MarkBarinstein:感谢SQL 更正
替代方案可能是
select t2.creator, t1.event_id, t2.status, t2.createdTS
from event T1
inner join event_to_event_status T2
on t1.event_id = t2.event_id
INNER JOIN (select event_id, creator, max(createdts) AS createdts
FROM event_to_event_status
GROUP BY event_id, creator) AS x
ON t2.event_id = x.event_id AND t2.creator = x.creator AND t2.createdts = x.createdts
我没有 iSeries 无法尝试,抱歉
以下 "as is" 在您的 IBM i 上是否有效?
WITH RES (creator, event_id, status, createdTS) AS
(
VALUES
(123, 1, 'New', TIMESTAMP('2019-01-29 17:27:50.899408'))
, (123, 1, 'Live', TIMESTAMP('2019-01-29 17:29:50.899408'))
, (123, 1, 'Closed', TIMESTAMP('2019-01-29 17:45:50.899408'))
, (456, 5, 'New', TIMESTAMP('2019-01-29 17:32:50.899408'))
, (456, 5, 'Live', TIMESTAMP('2019-01-29 17:48:50.899408'))
)
SELECT creator, event_id, status, createdTS
FROM
(
SELECT creator, event_id, status, createdTS
, ROWNUMBER() OVER (PARTITION BY creator ORDER BY createdTS desc) AS rn
FROM RES
) T
WHERE RN=1;
如果不是,那么您的 IBM i 版本和修改是什么?
我正在发布我的查询和结果,但我试图将其限制为仅包含创建者的最新状态和时间戳。
select t2.creator, t1.event_id, t2.status, max(t2.createdTS)
from schema.event T1
inner join schema.event_to_event_statust T2
on t1.event_id = t2.event_id
group by t2.creator, t1.event_id, t2.status;
creator | event_id | status | createdTS
-----------------------------------------------------
123 1 New 2019-01-29 17:27:50.899408
123 1 Live 2019-01-29 17:29:50.899408
123 1 Closed 2019-01-29 17:45:50.899408
456 5 New 2019-01-29 17:32:50.899408
456 5 Live 2019-01-29 17:48:50.899408
我想要的这组结果实际上是
creator | event_id | status | createdTS
-----------------------------------------------------
123 1 Closed 2019-01-29 17:45:50.899408
456 5 Live 2019-01-29 17:48:50.899408
所以每个创作者我应该只有一行,它应该是最新的记录。我做错了什么?
尝试
with temp as (
select t2.creator, t1.event_id, t2.status, t2.createdTS
row_number() over (partition by t2.creator order by t2.createdTS desc) as rownum
from schema.event T1
inner join schema.event_to_event_statust T2
on t1.event_id = t2.event_id
group by t2.creator, t1.event_id, t2.status
)
select creator, event_id, status, createdTS
from temp
where rownum = 1
@MarkBarinstein:感谢SQL 更正
替代方案可能是
select t2.creator, t1.event_id, t2.status, t2.createdTS
from event T1
inner join event_to_event_status T2
on t1.event_id = t2.event_id
INNER JOIN (select event_id, creator, max(createdts) AS createdts
FROM event_to_event_status
GROUP BY event_id, creator) AS x
ON t2.event_id = x.event_id AND t2.creator = x.creator AND t2.createdts = x.createdts
我没有 iSeries 无法尝试,抱歉
以下 "as is" 在您的 IBM i 上是否有效?
WITH RES (creator, event_id, status, createdTS) AS
(
VALUES
(123, 1, 'New', TIMESTAMP('2019-01-29 17:27:50.899408'))
, (123, 1, 'Live', TIMESTAMP('2019-01-29 17:29:50.899408'))
, (123, 1, 'Closed', TIMESTAMP('2019-01-29 17:45:50.899408'))
, (456, 5, 'New', TIMESTAMP('2019-01-29 17:32:50.899408'))
, (456, 5, 'Live', TIMESTAMP('2019-01-29 17:48:50.899408'))
)
SELECT creator, event_id, status, createdTS
FROM
(
SELECT creator, event_id, status, createdTS
, ROWNUMBER() OVER (PARTITION BY creator ORDER BY createdTS desc) AS rn
FROM RES
) T
WHERE RN=1;
如果不是,那么您的 IBM i 版本和修改是什么?