最近写了一个人数统计的系统,要求是列及行都是动态生成的,当有的人后续学历为空的时候,那么统计他的初始学历。比如说B的后续学历没有,那么统计他的初始学历。 
比如: 
数据库里面有个基础表A: 表A 姓名     部门    初始学历  后续学历     出生年月 
      A      后勤     高中       大学       1986-1-1 
      B      后勤     初中       null       1984-3-7 
      C      管理     本科       研究生     1987-2-1 
      D      操作     专科       大学       1976-2-1 
      .      .        .          .            。
      .      .        .          . 
要求动态的生成一下效果的表: 
部门  年龄段  高中  初中  本科....... 
后勤  20-30    3    4      3  ....... 
      30-40    3    9      0  ....... 
      40-50    0    30     3  ....... 
管理  20-30    9    2      2  ....... 不管后续学历是否为空,统计后续学历这一条件的存储过程我已经写完了,在sql里面执行成功了。 ALTER PROCEDURE  [dbo].[ps_statbumen3] 
@year nvarchar(4) 
AS 
declare @sql varchar(8000) set @sql = 'select department,年龄段' 
select @sql = @sql + ' , sum(case LatterEducation when ''' +  Educationtype + ''' then 1 else 0 end) [' + Educationtype+ ']' 
from (select distinct Educationtype from Educationtype) as a  
set @sql = @sql + ' from (select *,dbo.agetime1(Birthday) 年龄段 from v_hdemployee ) as tb where year='+@year+'  group by department,年龄段' exec(@sql) 请问当在统计后学学历时,有的人后续学历为空的时候,那么统计他的初始学历存储怎么写啊????  

解决方案 »

  1.   

    isnull(后续学历,初始学历)就可以搞定了
      

  2.   

    try
    ALTER PROCEDURE  [dbo].[ps_statbumen3]
    @year nvarchar(4)
    AS
    declare @sql varchar(8000)set @sql = 'select department,年龄段'
    select @sql = @sql + ' , sum(case isnull(LatterEducation,初始学历) when ''' +  Educationtype + ''' then 1 else 0 end) [' + Educationtype+ ']'
    from (select distinct Educationtype from Educationtype) as a 
    set @sql = @sql + ' from (select *,dbo.agetime1(Birthday) 年龄段 from v_hdemployee ) as tb where year='+@year+'  group by department,年龄段'exec(@sql) 
      

  3.   

    呵呵谢谢各位兄弟们!!! 谢谢josy...要是还要对学历按照 Educationtype表里面的id来排序那怎么排啊???