日期:2014-05-18 浏览次数:20581 次
declare @tb table (a int,b int,c varchar(20))
insert into @tb
select 1,3,null union all
select 2,3,null union all
select 3,4,null union all
select 4,5,null union all
select 5,6,null
declare @tb1 table (e varchar(8),col varchar(1))
insert into @tb1
select 15,'a' union all
select 20,'a' union all
select 15,'a' union all
select 20,'a' union all
select 36,'a' union all
select 36,'a' union all
select 36,'a' union all
select 48,'a' union all
select 51,'a' union all
select 51,'a' union all
select 61,'a'
update @tb set c=b.e from @tb a
left join(
select row_number() over (order by count (*) desc) as id, e
from @tb1 group by e) b on a.a=b.id
select * from @tb
/*
a b c
----------- ----------- --------------------
1 3 36
2 3 15
3 4 20
4 5 51
5 6 61
*/
--如果是空表直接插入的话
insert into @tb (c)
select e from @tb1 group by e order by count (*) desc