I am trying to achieve the following result:
and I am doing so with the following statement
select
  invoice_N,
  buyer,
  max(count_order)
from (
    select
      invoice_N,
      buyer,
      count(distinct OrderIdent) as count_order
    from invoices
    left join orders on ...
    group by invoice_N, buyer 
)a
group by invoice_N, buyer
which is not correct. how can I select only the buyer with most orders for each invoice?
thank you in advance and best regards

 
    