在postgres中,您可以查询组中的第一个值DISTINCT ON.如何在Oracle中实现这一目标?
来自postgres手册:
SELECT DISTINCT ON(expression [,...])仅保留给定表达式求值的每组行的第一行.使用与ORDER BY相同的规则解释DISTINCT ON表达式(参见上文).请注意,除非使用ORDER BY确保首先显示所需的行,否则每个集合的"第一行"都是不可预测的.
例如,对于给定的表:
col1 | col2
------+------
A | AB
A | AD
A | BC
B | AN
B | BA
C | AC
C | CC
Run Code Online (Sandbox Code Playgroud)
升序排序:
> select distinct on(col1) col1, col2 from tmp order by col1, col2 asc;
col1 | col2
------+------
A | AB
B | AN
C | AC
Run Code Online (Sandbox Code Playgroud)
降序排序:
> select distinct on(col1) col1, col2 from tmp order by col1, col2 desc; …Run Code Online (Sandbox Code Playgroud) 表基本上看起来像:
序列号,ID,日期,数据,数据,数据等
对于相同的ID,可以有多行.我想创建一个在Reports中使用的表的视图,它只显示每个ID的最新条目.它应该显示所有列.
有人可以帮助我选择SQL吗?谢谢.