![](/img/trans.png)
[英]SQL - Select distinct record in one column based on criteria in another
[英]SQL Select One Record over another based on column value
我想做的是基於“優先級”的 select 行。
說我有這個:
ControlID ProgramID Priority
1 4 0
1 4 1
2 4 0
我想為控件 ID(整行)各選擇一行,這將是第三行,因為沒有優先級,而第二行是因為它具有優先級。 因此,如果我有兩個相同的控件 ID,我要選擇的那個是具有“優先級”的那個。
所以我的結果是:
ControlID ProgramID Priority
1 4 1
2 4 0
我試過做一個子查詢,但我不擅長它們......
您可以使用row_number
來做到這一點:
with r as (
select
ControlId,
ProgramId,
Priority,
row_number() over(partition by ControlId order by Priority desc) rn
)
select
ControlId,
ProgramId,
Priority
from r
where rn = 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.