本帖最后由 chirea 于 2011-07-13 09:17:58 编辑

解决方案 »

  1.   

    小三
    就是要判断计算出来的年龄数在年限表里面那个范围
    如果小于最大值大于或等于最小值,就获取得小值的Allowance里面的值
      

  2.   

    是不是两个if语句的嵌套
    if(num>min(num))
     begin
      if(num<max(num))
       begin
       ...
       end
    end
      

  3.   

    create table #A(ID INT,SDATE DATETIME)
    INSERT INTO #A(ID,SDATE)
    VALUES(1,' 2001-02-13 09:06:15.287')
    ,(2,' 2003-02-13 09:06:15.287')
    ,(3,' 2006-02-13 09:06:15.287')select Allowance=
    (case when datepart(yy,getdate())-datepart(yy,SDATE) between 0 and 0.99999999 then 10 
      when datepart(yy,getdate())-datepart(yy,SDATE) between 1 and 2.99999999 then 40
      when datepart(yy,getdate())-datepart(yy,SDATE) between 3 and 4.99999999 then 60
     else 0 end
    ),  * from #a a
      

  4.   


    create table t1(ID int,进厂时间 datetime)
    insert into t1
    select 1,'2001-02-13 09:06:15.287' union all
    select 2,'2003-03-13 09:06:15.287' union all         
    select 3,'2006-06-13 09:06:15.287' union all
    select 4,'2010-06-13 09:06:15.287'
    gocreate table t2(ID int,WorkingAge int,Allowance int)
    insert into t2
    select 1,1,10 union all
    select 2,3,40 union all
    select 3,5,60
    go;with AcHerat as
    (
    select a.ID,a.进厂时间,b.WorkingAge,b.Allowance,datediff(yy,a.进厂时间,getdate()) yy,
    rid=row_number() over (partition by a.ID order by b.WorkingAge)
    from t1 a left join t2 b on datediff(yy,a.进厂时间,getdate()) >= b.WorkingAge
    )select ID,进厂时间,WorkingAge,Allowance,yy
    from AcHerat t
    where rid = (select max(rid) from AcHerat where ID = t.ID)drop table t1,t2
    /*************************ID          进厂时间                    WorkingAge  Allowance   yy
    ----------- ----------------------- ----------- ----------- -----------
    1           2001-02-13 09:06:15.287 5           60          10
    2           2003-03-13 09:06:15.287 5           60          8
    3           2006-06-13 09:06:15.287 5           60          5
    4           2010-06-13 09:06:15.287 1           10          1(4 行受影响)
    ????????
      

  5.   

    create table #A(ID INT,SDATE DATETIME)
    INSERT INTO #A(ID,SDATE)
    VALUES(1,' 2001-02-13 09:06:15.287')
    ,(2,' 2003-02-13 09:06:15.287')
    ,(3,' 2006-02-13 09:06:15.287')
    ,(4,' 2008-02-13 09:06:15.287')
    select *,( select top 1 Allowance  from #b where datepart(yy,getdate())-datepart(yy,SDATE)>=WorkingAge order by WorkingAge desc )
    from #A 
    ---------------------------------
    ID SDATE (无列名)
    1 2001-02-13 09:06:15.287 60
    2 2003-02-13 09:06:15.287 60
    3 2006-02-13 09:06:15.287 60
    4 2008-02-13 09:06:15.287 40
      

  6.   

    Create table #b(ID int,WorkingAge int, Allowance int)insert into #b values(1,1,10),(2,3,40),(3,5,60)
    忘记了这段代码
    其实你还少了个条件,就是如果一直大于5年的,最多也只能拿60块钱,如果小于1年的就没有钱拿,是这个意思不?
      

  7.   


    select id,get_in_time
    (select top 1  Allowance
    from WorkingAge_tab
    where WorkingAge>=(year(getdate())-year(get_in_time))
    order by WorkingAge desc
    ) as Allowance
    from get_in_tb 
      

  8.   

    小三你真的是太强了!!!!
    你的思路真的是好,再来排一次充用RID来判断.
      

  9.   

    小三create table t1(number nvarchar(30),PostID int,进厂时间 datetime)
    insert into t1
    select'001', 1,'2001-02-13 09:06:15.287' union all
    select'002', 3,'2003-03-13 09:06:15.287' union all         
    select'003', 2,'2006-06-13 09:06:15.287' union all
    select'004', 4,'2010-06-13 09:06:15.287'
    gocreate table t2(ID int,WorkingAge int,PostID int,Allowance int)
    insert into t2
    select 1,1,2,10 union all
    select 2,3,3,40 union all
    select 3,5,3,60
    go