27,579
社区成员
发帖
与我相关
我的任务
分享
declare @tb1 table (userid int,usersize int)
declare @tb2 table (userid int, region char(3))
insert into @tb1
select 1,1 union all
select 2,3 union all
select 3,3 union all
select 4,1 union all
select 5,2 union all
select 6,3 union all
select 7,2 union all
select 8,3 union all
select 9,3
insert into @tb2
select 1,'aaa' union all
select 2,'aaa' union all
select 3,'aaa' union all
select 4,'bbb' union all
select 5,'bbb' union all
select 6,'bbb' union all
select 7,'ccc' union all
select 8,'ccc' union all
select 9,'ccc'
select num, region from (
select count(userid) as num,
region,
dense_rank() over(order by count(userid) desc) as rank_no
from @tb2
where userid in (
select userid
from @tb1
where usersize = 3)
group by region
) aaa
where aaa.rank_no = 1
num region
----------- ------
2 aaa
2 ccc
(2 行処理されました)
with cte as
(
select userid,usersize,COUNT(*) as 个数
from table1
group by userid,usersize)
select region
from table2 join cte a on table2.userid=a.userid
where usersize=3 and not exists(select * from cte where a.userid=userid and 个数>a.个数 )
select
top 1 region,count(1) as num
from
table1 t1
join table2 t2 on
t1.userid=t2.userid
where
t1.usersize=3
group by
region
order by
count(1) desc
select
MAX( t2.region)
from
table1 t1,table2 t2
where
t1.userid=t2.userid
and
t1.usersize=3
group by
t2.region
select top 1 n.region , count(1) cnt from table1 m , table2 n where m.userid = n.userid and m.usersize = 3 group by n.region order by cnt desc
select top 1 t2.region,count(1)
from table1 t1,table2 t2
where t1.userid=t2.userid
and t1.usersize=3
group by t2.region
order by count(1) desc
select top 1 region,数量=count(1)
from table1 t1 A join table2 t2 B
on A.userid=B.userid
where A.usersize=3
group by region
order by count(1) desc
select top 1 t2.region
from table1 t1,table2 t2
where t1.userid=t2.userid
and t1.usersize=3
group by t2.region
order by count(1) desc