尝试这个
样本输入:(案例 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