Это называется режим .Вы можете использовать оконные функции:
select o.*
from (select os, device, count(*) as cnt,
row_number() over (partition by os order by count(*) desc) as seqnum
from DeviceOS2
group by os, device
) o
where seqnum = 1;
Если вы хотите наиболее частую комбинацию, используйте:
select os, device, count(*) as cnt
from DeviceOS2
group by os, device
order by count(*) desc
fetch first 1 row only;
(или используйте select top (1)
, если хотите).
РЕДАКТИРОВАТЬ:
Для вашего отредактированного вопроса:
select o.*
from (select os, device, count(*) as cnt,
row_number() over (partition by os order by count(*) desc) as seqnum
from DeviceOS2
group by os, device
) o
where seqnum = 1;
Если вы хотите наиболее частую комбинацию, тогда запрос немного сложнее.Один метод состоит из двух агрегаций:
select o.id,
max(case case when o.seqnum = 1 then os end) as most_frequent_os,
max(case case when d.seqnum = 1 then device end) as most_frequent_device
from (select id, os, count(*) as cnt,
row_number() over (partition by id order by count(*) desc) as seqnum
from DeviceOS2
group by id, os
) o join
(select id, device, count(*) as cnt,
row_number() over (partition by id order by count(*) desc) as seqnum
from DeviceOS2
group by id, device
) d
on d.id = o.id