年龄段:
((year(time1)*10000+month(time1)*100+day(time1))-
(year(time2)*10000+month(time2)*100+day(time2)))
/2000算出来是每5年一个年龄段
精确到天。

解决方案 »

  1.   

    如果上面看着不习惯,这样算出就是年龄了((year(time1)*10000+month(time1)*100+day(time1))-
    (year(time2)*10000+month(time2)*100+day(time2)))
    /10000如果上面看着不习惯,这样算出就是年龄了
      

  2.   

    sky_blue(老衲),你看一下SQL Server的联机帮助,找datediff,你会发现你这么麻烦做的事情,其实已经有内部函数了。bjhb2004 (),你可以建一个自定义函数。
      

  3.   

    sky_blue(老衲),datediff可以精确到天,甚至到毫秒。另外,我感觉楼主不是要精确到天,而是几年而已。
      

  4.   

    sky_blue(老衲),sorry,仔细看你在1楼的回答,确实能够部分达到楼主的要求:精确到5年。但是29以下和55以上则无法处理。
    所以我觉得还是要用自定义函数。
      

  5.   

    select count(*)
    from 表
    group by case when 年龄<30 then -1 when 年龄>59 then 0 else
             floor(年龄/5) end
      

  6.   

    Chiff(~o~) 强!佩服一个。楼主刻意结贴了。
      

  7.   

    Chiff(~o~) 的方法不错呀,;)Studying
      

  8.   

    select agegrand,count(userid)
    from (
    select userid, agegrand=
    case
    when datediff(year,[生日字段],getdate()) <30 then 0
    when datediff(year,[生日字段],getdate()) between 30 and 34 then 1
    when datediff(year,[生日字段],getdate()) between 35 and 39 then 2
    ...
    when datediff(year,[生日字段],getdate()) between 50 and 54 then 5
    else 6 end
    from 表
    ) t1
    group by agegrand
      

  9.   

    when datediff(year,[生日字段],getdate()) between 30 and 34 then 1
    when datediff(year,[生日字段],getdate()) between 35 and 39 then 2
    ...
    when datediff(year,[生日字段],getdate()) between 50 and 54 then 5可以简化为:
    when datediff(year,[生日字段],getdate()) between 30 and 54 then (datediff(year,[生日字段],getdate()) -25)/5