これを試して
サンプル入力:(ケース 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