答案 0 :(得分:2)
这是a special case of a top N per category
style query,可以在Oracle中高效实施。
FIRST
function SELECT
id,
MIN(name) KEEP (DENSE_RANK FIRST ORDER BY starttime) name,
MIN(starttime) KEEP (DENSE_RANK FIRST ORDER BY starttime) starttime
FROM t
GROUP BY id
其他解决方案包括:
SELECT id, name, starttime
FROM (
SELECT
id, name, starttime,
ROW_NUMBER () OVER (PARTITION BY id ORDER BY starttime) rn
FROM t
) t
WHERE rn = 1
As suggested by Yogesh,但它们比上述解决方案慢。
答案 1 :(得分:2)
使用subquery
或排名功能:
select *
from table t
where StartTime = (select min(StartTime) from table where Id = t.Id);
您还可以使用row_number
排名功能
select *
from (select *,
row_number() over (partition by Id order by StartTime) Seq
from table t
) t
where Seq = 1;