我有两行重复两行,但该行的一列具有空值,另一列具有某个值。例如
CarName Owner PreviousOwner
Honda XXX YYYYY
Honda XXX
Nissan ZZZ AAAA
Nissan ZZZ
BMW BBB
Benz CCC DDD
输出应该是
Honda XXX YYYYY
Nissan ZZZ AAAA
BMW BBB
Benz CCC DDD
能帮我写一个对此的查询吗?
答案 0 :(得分:1)
检查
select CarName, Owner, max(PreviousOwner)
from t
group by CarName, Owner
答案 1 :(得分:1)
如果我们要对记录进行排序,以使前一个拥有值的所有者来 在最上面,那么您要做的就是选择最上面的行。为了实现这一点,我使用了row_number()以及previous_owner的顺序。请看下面:
select carname, owner, previous_owner from
(select cars.*, rowid, row_number() over (partition by carname, owner order by
previous_owner) rid
from cars)
where rid <=1;
答案 2 :(得分:0)
一种方法是:
select t.CarName, t.Owner, r.PreviousOwner
from (select Carname, max(previousowner)
from test
group by carname) r
inner join test t
on t.carname = r.carname and t.previousowner = r.previousowner
group by t.CarName, t.Owner, r.PreviousOwner;