您可以查詢中使用解決了這個連接和window functions:
select nodups.id, nodups.fname, nodups.lname, d1.data1, d2.data2
from
(select min(id) as id, fname, lname from sample group by fname, lname) nodups
left join
(select fname, lname, min(data1) as data1
from (select fname, lname
, first_value(data1) over (partition by fname, lname order by id) as data1
from sample where data1 is not null) d1x
group by fname, lname
) d1 using (fname, lname)
left join
(select fname, lname, min(data2) as data2
from (select fname, lname
, first_value(data2) over (partition by fname, lname order by id) as data2
from sample where data2 is not null) d2x
group by fname, lname
) d2 using (fname, lname)
order by id
;
SQLFiddle
嘗試用你的真實數據測試這種方法對伊戈爾的自定義聚合看執行更好。
這種方法就像一個魅力,謝謝你! – BrunoJ