2

我的查询类似于:

select
  name,
  color,
  min(age) keep (dense_rank first order by priority asc) as age
from
  myTable
group by
  name, 
  color

我知道 rank 和 dense_rank 将在分区中返回“重复”值。就我而言,如果优先级在 2 个年龄段之间共享。

那么这条线会做什么:

  min(age) keep (dense_rank first order by priority asc) as age

它会为给定的名称、颜色选择优先级最低的行并返回相应的年龄吗?如果有两个具有相同优先级的行,它会随机选择其中一个行吗?

4

2 回答 2

3

在您的示例中,如果列表中的第一个优先级有多个与之关联的年龄值,则它min(age)决定了要显示哪个。max(age)如果您希望显示最高年龄而不是最低年龄,则可以将其换掉。

于 2015-02-18T22:52:28.063 回答
3

对于给定的名称和颜色,它将找到优先级最低的行,并在这些行中找到最低的年龄。看到这个,从你的例子中很清楚地填充了一些数据

http://sqlfiddle.com/#!4/3d765f/2

于 2015-02-18T23:16:55.560 回答