解决方案 »

  1.   

    临时表数据根据你需求来,王五不知道怎么弄的 是不是有人员入职时间啊? 要是有入职日期 做存储过程 每次查询先通过入职日期————当前日期做临时表 create table test(人员 varchar(10),部门 varchar(10),年份 varchar(10),季度 int)
    insert test 
    select '张三','a部门','2010',1 union all
    select '张三','a部门','2013',1 union all
    select '李四','b部门','2013',2 union all
    select '李四','b部门','2014',6 union all
    select '张三','b部门','2013',1 create table #tableA (年份 varchar(10),季度 int)
    insert #tableA
    select '2010',1 union all 
    select '2010',2 union all 
    select '2010',3 union all 
    select '2010',4 union all 
    select '2011',1 union all 
    select '2011',2 union all 
    select '2011',3 union all 
    select '2011',4 union all 
    select '2012',1 union all 
    select '2012',2 union all 
    select '2012',3 union all 
    select '2012',4 union all 
    select '2013',1 union all 
    select '2013',2 union all 
    select '2013',3 union all 
    select '2013',4 union all 
    select '2014',1DECLARE @人员 varchar(10)
    DECLARE @部门 varchar(10)
    set @人员 ='张三'
    set @部门 ='a部门'
    select 人员=@人员,部门=@部门,a.年份,a.季度
    from #tableA a
    left join (select * 
    from test 
    where 人员=@人员 and 部门=@部门
      ) b
    on a.年份=b.年份
    and a.季度=b.季度
    where b.人员 is null
      

  2.   


    create table test(人员 varchar(10),部门 varchar(10),年份 varchar(10),季度 int)
    insert test 
    select '张三','a部门','2010',1 union all
    select '张三','a部门','2013',1 union all
    select '李四','b部门','2013',2 union all
    select '李四','b部门','2014',6 union all
    select '张三','b部门','2013',1 IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('testproc') AND type = 'P')
    DROP PROC testproc
    GO
    create proc testproc
    @人员 varchar(10),
    @部门 varchar(10)
    as
    begin
    DECLARE @num int
    set @num=(select COUNT (*) from test where 人员=@人员 and 部门=@部门)
    if @num<1
    select @人员,@部门,datepart(YEAR,GETDATE()),datepart(q,GETDATE())
    else
     begin
    WITH 季度 AS ( SELECT number
       FROM     master..spt_values
       WHERE    type = 'P'
    AND number BETWEEN 1 AND 4
    ),
    年份 as(SELECT number
       FROM     master..spt_values
       WHERE    type = 'P'
    AND number BETWEEN (select MIN(年份)
    from test 
    where 人员=@人员 and 部门=@部门
    ) AND (select datepart(YEAR,GETDATE()))
    )
    select 人员=@人员,
    部门=@部门,
    a.年份,a.季度
    from (
    select 年份=年份.number,季度=季度.number
    from 年份,季度
    ) a
    left join (select * 
    from test 
    where 人员=@人员 and 部门=@部门
      ) b
    on a.年份=b.年份
    and a.季度=b.季度
    where b.人员 is null 
     end 
    end/*
    exec testproc '张三','a部门'
    张三 a部门 2010 2
    张三 a部门 2010 3
    张三 a部门 2010 4
    张三 a部门 2011 1
    张三 a部门 2011 2
    张三 a部门 2011 3
    张三 a部门 2011 4
    张三 a部门 2012 1
    张三 a部门 2012 2
    张三 a部门 2012 3
    张三 a部门 2012 4
    张三 a部门 2013 2
    张三 a部门 2013 3
    张三 a部门 2013 4
    张三 a部门 2014 1
    张三 a部门 2014 2
    张三 a部门 2014 3
    张三 a部门 2014 4
    exec testproc '张三aa','a部门'
    张三aa a部门 2014 2
    */