日期:2014-05-18 浏览次数:20725 次
select a.name,产品数=SUM(CASEWHEN type1='产品推广' THEN 1 ELSE 0 END),
业务数=SUM(........='业务联络' THEN 1 ELSE 0 END),
...略
FROM staff a
LEFT JOIN work b
ON a.name=b.staffname
GROUP BY a.name
------解决方案--------------------
select a.name,count(isnull(select 1 from work where staffname=a.name and type1='产品推广'),0)) as 产品推广数, count(isnull(select 1 from work where staffname=a.name and type1='业务联络'),0)) as 业务联络数, count(isnull(select 1 from work where staffname=a.name and type1='产品销售'),0)) as 产品销售数 from staff a group by a.name
------解决方案--------------------
同意楼上
------解决方案--------------------
declare @staff table(id int,name varchar(50))
insert into @staff select 1,'张三'
insert into @staff select 2,'李四'
insert into @staff select 3,'王五'
insert into @staff select 4,'赵六'
declare @work table(id int,staffname varchar(50),type varchar(50))
insert into @work select 1,'李四','产品推广'
insert into @work select 2,'李四','产品推广'
insert into @work select 3,'王五','业务联络'
insert into @work select 4,'王五','产品推广'
insert into @work select 5,'张三','销售产品'
select a.name,
sum(case when type='产品推广' then 1 else 0 end) as '产品推广数',
sum(case when type='业务联络' then 1 else 0 end) as '业务联络数',
sum(case when type='产品销售' then 1 else 0 end) as '产品销售数'
from @staff a left join @work b on a.name=b.staffname
group by a.id,a.name order by a.id