我有如下数据。我只想选择一个优先级较高的task_id
。
TASK_ID : 13912 = 2 rows
13913 = 3 rows
Task_ID owner priority
13912 JUNGIL 99
13912 BANCS 1
13913 JUNGIL 99
13913 BANCS 1
13913 AIL 2
还有更多数据,但我必须只选择一个。
答案 0 :(得分:0)
keep ... dense_rank first
(thanks for the reminder Gordon Linoff)的一些急需的练习:
select task_id,
max(owner) keep (dense_rank last order by priority) as owner,
max(priority) keep (dense_rank last order by priority) as priority
from t42
group by task_id
order by task_id;
TASK_ID OWNER PRIORITY
---------- ---------- ----------
13912 JUNGIL 99
13913 JUNGIL 99
如果最高优先级为1则为:
select task_id,
max(owner) keep (dense_rank first order by priority) as owner,
max(priority) keep (dense_rank first order by priority) as priority
from t42
group by task_id
order by task_id;
TASK_ID OWNER PRIORITY
---------- ---------- ----------
13912 BANCS 1
13913 BANCS 1
您也可以使用子查询和分析来执行此操作:
select task_id, owner, priority
from (
select task_id, owner, priority,
dense_rank() over (partition by task_id order by priority) as rnk
from t42
)
where rnk = 1
order by task_id;
TASK_ID OWNER PRIORITY
---------- ---------- ----------
13912 BANCS 1
13913 BANCS 1
...但keep
方法更简单。 SQL Fiddle显示两者。
答案 1 :(得分:0)
您似乎只需要group by
这样的查询:
select max(priority), task_id
from <YOUR_TABLE_HERE>
group by t.task_id
如果你想拥有所有行的列
select t.*
from t
join (
select max(priority) priority, task_id
from t
group by t.task_id
)t2 on t.task_id = t2.task_id and t.priority = t2.priority