------解决方案--------------------
/* ID COL 1 A 1 B 1 A 2 A 2 C */ /* 现有表T1,T1 分别有列A(1,2,3,4,5),B(a,a,b,a,b,c,a,b,c,d) 现所有a 对应1,b对应2... 现要在新表中输出 1 count(a) 2 count(b)... 请问sql语句怎么写 */ go if OBJECT_ID('A')is not null drop table A go create table A( cola int ) insert A select 1 union all select 2 union all select 3 union all select 4 union all select 5
go if OBJECT_ID('B')is not null drop table B go create table B( colb varchar(1) ) go insert B select 'a' union all select 'a' union all select 'b' union all select 'a' union all select 'b' union all select 'c' union all select 'a' union all select 'b' union all select 'c' union all select 'd' select A.cola,C.个数 from A inner join (select ROW_NUMBER()over(order by getdate()) as num,COUNT(colb) as 个数 from B group by colb)c on A.cola=c.num /* cola 个数 1 4 2 3 3 2 4 1 */