现有3个表:
A表:
id    name      
001   苏州  
002   杭州        
003   郑州   
004   常州
.......B表:
   
pno   cs 
1    张三
2    李四
3    王二
4    麻子
......
   
C表
加工度   区域代码     人员代码     状况
P0      001            1       N
P1      001            1       N
P2      003            3       Y
P1      004            2       C
P3      001            1       C
P0      003            2       Y
P2      001            1       c
.......希望查询结果:
                        P0          P1          P2           P3          合计
    区域     人员     Y  N  C    Y  N  C     Y  N  C      Y  N  C      Y   N   C
    苏州     张三        1          1              1             1     0   2  2以上我只是一个例子。
简而言之:3个表是相互关联, 要查询的结果是  每个区域所对应的人员 以及相应的加工度的状况的个数,还有合计出每个状况的 总和   
怎么分类统计啊  这个太复杂了,偶不会!!!sql分类

解决方案 »

  1.   


    是这样吗:create table A(id  int,   name varchar(20))insert into A    
    select '001',   '苏州'  
    union all select '002','杭州'        
    union all select '003','郑州'   
    union all select '004','常州'create table B(pno int,   cs varchar(20))insert into B 
    select 1,    '张三'
    union all select 2,   '李四'
    union all select 3,   '王二'
    union all select 4,   '麻子'
          
    create table C(
    加工度 varchar(20),
    区域代码 varchar(10),    
    人员代码 int,    
    状况 varchar(10)
    )insert into c
    select 'P0' ,  '001',            1,       'N'
    union all select 'P1'   ,'001',            1,       'N'
    union all select 'P2'   ,'003',            3,       'Y'
    union all select 'P1'   ,'004',            2,       'C'
    union all select 'P3'   ,'001',            1,       'C'
    union all select 'P0'   ,'003',            2,       'Y'
    union all select 'P2'   ,'001',            1,       'c'
    select a.name,
           b.cs,
           count(case when c.加工度 = 'P0' AND c.状况='Y' then 1 else null end) as P0_Y,
           count(case when c.加工度 = 'P0' AND c.状况='N' then 1 else null end) as P0_N,
           count(case when c.加工度 = 'P0' AND c.状况='C' then 1 else null end) as P0_C,              count(case when c.加工度 = 'P1' AND c.状况='Y' then 1 else null end) as P1_Y,
           count(case when c.加工度 = 'P1' AND c.状况='N' then 1 else null end) as P1_N,
           count(case when c.加工度 = 'P1' AND c.状况='C' then 1 else null end) as P1_C,
           
           count(case when c.加工度 = 'P2' AND c.状况='Y' then 1 else null end) as P2_Y,
           count(case when c.加工度 = 'P2' AND c.状况='N' then 1 else null end) as P2_N,
           count(case when c.加工度 = 'P2' AND c.状况='C' then 1 else null end) as P2_C,
           
           count(case when c.加工度 = 'P3' AND c.状况='Y' then 1 else null end) as P3_Y,
           count(case when c.加工度 = 'P3' AND c.状况='N' then 1 else null end) as P3_N,
           count(case when c.加工度 = 'P3' AND c.状况='C' then 1 else null end) as P3_C,
           
           count(case when c.加工度 in ('P0','P1','P2','P3') AND c.状况='Y' then '1' else null end) as 总计_Y,
           count(case when c.加工度 in ('P0','P1','P2','P3') AND c.状况='N' then 1 else null end) as 总计_N,
           count(case when c.加工度 in ('P0','P1','P2','P3') AND c.状况='C' then 1 else null end) as 总计_C     
    from c
    inner join a
            on a.id = c.区域代码
    inner join b
            on b.pno = c.人员代码
    group by a.name,b.cs
    /*
    name cs P0_Y P0_N P0_C P1_Y P1_N P1_C P2_Y P2_N P2_C P3_Y P3_N P3_C 总计_Y 总计_N 总计_C
    常州 李四 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1
    郑州 李四 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0
    郑州 王二 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0
    苏州 张三 0 1 0 0 1 0 0 0 1 0 0 1 0 2 2
    */
      

  2.   

    TO:
       yupeigu 
       阳泉酒家小当家    
        
       非常非常感谢你的帮助,受益匪浅! 谢谢
      

  3.   

    我写个动态的吧
    declare @s varchar(max),@s1 varchar(max)
    set @s=''
    set @s1=''
    select @s=@s+','+QUOTENAME(加工度+状况)+'=sum(case when 加工度+状况='+QUOTENAME(加工度+状况,'''')+'then 1 else 0 end)'
           from C group by 加工度+状况
    select @s1=@s1+','+QUOTENAME('Total'+状况)+'=sum(case when 状况='+quotename(状况,'''')+'then 1 else 0 end)' from C group by 状况
    exec('select a.name,b.cs'+@s+@s1+ ' from A,B,C where a.id=c.区域代码 and b.pno=c.人员代码 group by a.name,b.cs')