日期:2014-05-17 浏览次数:20541 次
部门 所有部门 部门A 部门B 部门C
A级 B级 C级 D级 A级 B级 C级 D级 A级 B级 C级 D级 A级 B级 C级 D级
次数 0 0 1 1 0 0 0 1 0 0 1 0 0 0 0 0
DECLARE @t1 TABLE(ljbz INT ,bzName VARCHAR(10));
DECLARE @t2 TABLE(id int,Dname VARCHAR(10),bzname VARCHAR(10),wt VARCHAR(4));
INSERT INTO @t1 SELECT 101,'A级' UNION ALL
SELECT 102,'B级' UNION ALL
SELECT 103,'C级' UNION ALL
SELECT 104,'D级';
INSERT INTO @t2 SELECT 1,'部门A','D级','是'
UNION ALL SELECT 2,'部门B','C级','是'
UNION ALL SELECT 3,'部门C','A级','否';
SELECT [部门A-A级] as [部门A-A级],[部门A-B级] as [部门A-B级],[部门A-C级] as [部门A-C级],[部门A-D级] as [部门A-D级],[部门B-A级] as [部门B-A级],[部门B-B级] as [部门B-B级],[部门B-C级] as [部门B-C级],[部门B-D级] as [部门B-D级],[部门C-A级] as [部门C-A级],[部门C-B级] as [部门C-B级],[部门C-C级] as [部门C-C级],[部门C-D级] as [部门C-D级] FROM (
SELECT a.Dname+'-'+b.bzName AS NAME,CASE WHEN a.wt='是' AND a.bzname=b.bzName THEN 1 ELSE 0 END AS s FROM @t2 a FULL JOIN @t1 b ON 1=1 --ORDER BY a.Dname,b.bzname
) p PIVOT(SUM(s) FOR NAME IN ([部门A-A级],[部门A-B级],[部门A-C级],[部门A-D级],[部门B-A级],[部