日期:2014-05-18 浏览次数:20830 次
declare @表1 table (col varchar(1),name varchar(4)) insert into @表1 select 'a','北京' union all select 'b','重庆' union all select 'c','上海' declare @表2 table (id int,col varchar(1)) insert into @表2 select 1,'a' union all select 2,'b' union all select 3,'a' union all select 4,'c' union all select 5,'c' select distinct b.name, 次数='出现'+ltrim((select count(1) from @表2 where col=a.col))+'次' from @表2 a left join @表1 b on a.col=b.col /* name 次数 ---- ------------------ 北京 出现2次 上海 出现2次 重庆 出现1次 */
------解决方案--------------------
create table T1 (col varchar(1),name varchar(4)) insert into T1 select 'a','北京' union all select 'b','重庆' union all select 'c','上海' create table T2 (id int,col varchar(1)) insert into T2 select 1,'a' union all select 2,'b' union all select 3,'a' union all select 4,'c' union all select 5,'c' select T1.[name],'出现'+convert(varchar(10),count(1))+'次' from T1,T2 where T1.col=t2.col group by T1.[name]
------解决方案--------------------
create table 表1 (col varchar(1),name varchar(4))
insert into 表1
select 'a','北京' union all
select 'b','重庆' union all
select 'c','上海'
create table 表2 (id int,col varchar(1))
insert into 表2
select 1,'a' union all
select 2,'b' union all
select 3,'a' union all
select 4,'c' union all
select 5,'c'
select a.name,'出现'+cast(count(*) as varchar(5))+'次' as 次数 from 表1 a join 表2 b on a.col=b.col group by a.name
结果为:
name 次数
北京 出现2次
上海 出现2次
重庆 出现1次
(3 行受影响)