根据优先级 ID 从集合中获取最低日期

Get the lowest date out of a set based on priority ID

我有以下优先级列表table,其中 Source4 优先于 3,3 优先于 2,2 优先于 1,依此类推。

SourceID SourceDescription
1 Source1
2 Source2
3 Source3
4 Source4

我还有以下 table 数据(但是 table 将包含多个不同的 EventID)

EventID CommencingTime SourceID
12345 2021-10-24 11:27:34 1
12346 2021-10-24 11:27:34 1
12347 2021-10-24 11:27:34 1
12345 2021-10-24 12:58:55 3
12346 2021-10-24 12:58:55 3
12347 2021-10-24 12:58:55 3
12345 2021-10-24 10:58:00 2
12346 2021-10-24 10:58:00 2
12347 2021-10-24 10:58:00 2

如何根据优先级列表从上面得到最低的日期。

在这种情况下,正确的结果集将是:

EventID CommencingTime SourceID
12345 2021-10-24 12:58:55 3
12346 2021-10-24 12:58:55 3
12347 2021-10-24 12:58:55 3

我试过 MIN OVER PARTITION BY ORDER BY SourceID DESC,但它一直返回 2021-10-24 10:58:00 并忽略优先级 SourceID

您可以使用 rank 根据您共享的示例实现此目的

select 
    EventID,CommencingTime,SourceID
from (
    select
        *,
        rank() over (order by SourceID DESC,CommencingTime) as rn
    from
       mytable
) t
where rn=1

View working demo here