最近写了一个人数统计的系统,要求是列及行都是动态生成的,当有的人后续学历为空的时候,那么统计他的初始学历。比如说B的后续学历没有,那么统计他的初始学历。 
比如: 
数据库里面有个基础表A: 表HdEmployee 姓名    部门    初始学历  后续学历    出生年月 
              A      后勤    高中      大学      1986-1-1 
              B      后勤    初中      null      1984-3-7 
              C      管理    本科      研究生    1987-2-1 
              D      操作    专科      大学      1976-2-1 
              .      .        .          .            。 
              .      .        .          . 
  要求动态的生成一下效果的表: 
    
    学历   人数
    大学    5
    中学    6
    本科    7
     。     。不管后续学历是否为空,统计后续学历的个各学历的人数这一条件的存储过程我已经写完了,在sql里面执行成功了。ALTER  PROCEDURE  [dbo].[ps_Statisticslattereducation]
@year int

AS
         select  EducationType.EducationType as lattereducation, count(*)as count
from EducationType, HdEmployee
where year = @year  and EducationType.id = HdEmployee.LatterEducation
group by EducationType.EducationType, EducationType.id
order by EducationType.id;
return  ;
请问当在统计后学学历时,有的人后续学历为空的时候,那么统计他的初始学历存储怎么写啊????  

解决方案 »

  1.   

    isnull(初始学历,后续学历 )
      

  2.   


    isnull(后续学历,初始学历)
      

  3.   

    select count(*),isnull( 后续学历,  初始学历 )
     group by isnull( 后续学历,  初始学历 )
      

  4.   

    --> 测试数据:@tb
    declare @tb table([姓名] varchar(1),[部门] varchar(4),[初始学历] varchar(4),[后续学历] varchar(6),[出生年月] datetime)
    insert @tb
    select 'A','后勤','高中','大学','1986-1-1' union all
    select 'B','后勤','初中',null,'1984-3-7' union all
    select 'C','管理','本科','研究生','1987-2-1' union all
    select 'D','操作','专科','大学','1976-2-1'select count(*),isnull( 后续学历,  初始学历 )
    from @tb 
    group by isnull( 后续学历,  初始学历 )
    /*
    ----------- ------
    1           初中
    2           大学
    1           研究生(3 行受影响)
    */
      

  5.   

    谢谢。Beirut!! 我还有一个学历表里面的学历对应id,统计出来的表格里学历要按照学历的ID来排序,那怎么去写啊???学历表
    学历   id
    高中    1
    大学    2
    专科    3                  
      

  6.   

     
    就跟这个存储过程差不多的:select  EducationType.EducationType as lattereducation, count(*)as count 
    from EducationType, HdEmployee 
    where year = @year  and EducationType.id = HdEmployee.LatterEducation 
    group by EducationType.EducationType, EducationType.id 
    order by EducationType.id; HdEmployee 表里面的学历是学历id,不是汉字