请考虑下表:
primaryKey id activity template creator created
1 1 3 5 x 2011-10-13
2 2 4 2 y 2011-10-15
3 2 4 7 z 2011-10-24
4 2 4 7 u 2011-10-29
Run Code Online (Sandbox Code Playgroud)
从这里我想要检索具有唯一组合的记录id,activity和template.如果存在两个或更多这些字段的唯一组合,我想采取它们中的第一个.
作为上表数据的一个例子,我需要的输出是
primaryKey id activity template creator created
1 1 3 5 x 2011-10-13
2 2 4 2 y 2011-10-15
3 2 4 7 z 2011-10-24
Run Code Online (Sandbox Code Playgroud)
(因为记录3和4具有相同的组合,我想只记录3,因为它是第一次出现)
我可以使用单个SQL语句执行此操作吗?
SELECT primarykey, id, activity, template, creator, created FROM (
SELECT *, row_number() OVER (partition BY id, activity, template ORDER BY created) as rn FROM table
) a
WHERE rn = 1
Run Code Online (Sandbox Code Playgroud)