Попробуйте это
Пример ввода: (Случай 1)
declare @t table(Typeid int,ObjectId int)
insert into @t
select 1,10 union all select 2,10 union all
select 1,11
select * from @t
Пример ввода: (Случай 2)
declare @t table(Typeid int,ObjectId int)
insert into @t
select 1,10 union all select 2,10 union all
select 3,10 union all select 4,10 union all
select 5,10 union all select 6,10 union all
select 1,11 union all select 2,11 union all
select 3,11 union all select 4,11 union all
select 5,11 union all select 1,12 union all
select 2,12 union all select 3,12 union all
select 4,12 union all select 5,12 union all
select 6,12
select * from @t
Пример ввода: (Случай 3) [Дубликаты записей есть]
declare @t table(Typeid int,ObjectId int)
insert into @t
select 1,10 union all select 2,10 union all
select 1,10 union all select 2,10 union all
select 3,10 union all select 4,10 union all
select 5,10 union all select 6,10 union all
select 1,11 union all select 2,11 union all
select 3,11 union all select 4,11 union all
select 5,11 union all select 1,12 union all
select 2,12 union all select 3,12 union all
select 4,12 union all select 5,12 union all
select 6,12 union all select 3,12
Для случая 1 вывод должен быть 10
Для случаев 2 и 3 выходное значение должно быть 10 и 12
Запрос:
select X.ObjectId from
(
select
T.ObjectId
,count(ObjectId) cnt
from(select distinct ObjectId,Typeid from @t)T
where T.Typeid in(select Typeid from @t)
group by T.ObjectId )X
join (select max(Typeid) maxcnt from @t)Y
on X.cnt = Y.maxcnt