答案 0 :(得分:0)
可以使用case
中的order by
表达式和row_number
之类的函数来优先考虑这些条件。
select A,B,frequency,timekey
from (select t.*
,row_number() over(partition by A order by cast((B = 'unknown') as int), B) as rnum
from tbl t
) t
where rnum = 1
在这里,对于每组A行,我们首先对B = 'unknown'
以外的行进行优先级排序,然后按B
值的顺序进行优先级排序。
答案 1 :(得分:0)
使用row_number
分析函数。如果您不想先选择unknown
条记录,请使用以下查询:
select A, B, Frequency, timekey
from
(select
A, B, Frequency, timekey,
row_number() over(partition by A,Frequency order by case when B='unknown' then 1 else 0 end) rn
)s where rn=1
如果要选择unknown
(如果存在),请在上面的查询中使用此row_number
:
row_number() over(partition by A,Frequency order by case when B='unknown' then 0 else 1 end) rn