第一个问题:表里有个字段:birthdate
想根据出生日期,按照5岁进行分组,如:
10岁以下
10-14岁
15-19岁
20-24岁
25-29岁
30岁以上我发现如果通过group by datediff(yyyy,birthdate,getdate()/5 进行分组
结果把15岁的也分到10-14岁了,结果很不准确。
请教该如何写?
第二个问题:
sql server默认一星期从周日算起,我需要通过语句在存储过程里面,改成从周一算起?
语句怎么写?谢谢

解决方案 »

  1.   

    SET DATEFIRST
    将一周的第一天设置为从 1 到 7 之间的一个数字。语法
    SET DATEFIRST { number | @number_var } 
      

  2.   


    select *,(case  when datediff(yyyy,birthdate,getdate())<10 then '10岁以上'
     when datediff(yyyy,birthdate,getdate()) between 10 and 14 then '10-14岁'
     when datediff(yyyy,birthdate,getdate()) between 10 and 14 then '15-19岁'
     when datediff(yyyy,birthdate,getdate()) between 10 and 14 then '20-24岁'
     when datediff(yyyy,birthdate,getdate()) between 10 and 14 then '25-29岁'
     else '30岁以上' end) felei
    from tb order by birthdate desc
      

  3.   

    select *,(case  when datediff(yyyy,birthdate,getdate())<10 then '10岁以下'
     when datediff(yyyy,birthdate,getdate()) between 10 and 14 then '10-14岁'
     when datediff(yyyy,birthdate,getdate()) between 15 and 19 then '15-19岁'
     when datediff(yyyy,birthdate,getdate()) between 20 and 24 then '20-24岁'
     when datediff(yyyy,birthdate,getdate()) between 25 and 29 then '25-29岁'
     else '30岁以上' end) felei
    from tb order by birthdate desc
      

  4.   

    select *,(case  when datediff(yyyy,birthdate,getdate())<10 then '10岁以下'
     when datediff(yyyy,birthdate,getdate()) between 10 and 14 then '10-14岁'
     when datediff(yyyy,birthdate,getdate()) between 15 and 19 then '15-19岁'
     when datediff(yyyy,birthdate,getdate()) between 20 and 24 then '20-24岁'
     when datediff(yyyy,birthdate,getdate()) between 25 and 29 then '25-29岁'
     else '30岁以上' end) felei
    from tb order by birthdate desc------------------
    把所有的datediff(yyyy,birthdate,getdate())
    替换成
    datediff(YEAR,birthdate,getdate())+ 
    (CASE WHEN (DATEADD(YEAR,datediff(YEAR,birthdate,getdate()),birthdate))>GETDATE() THEN 1 ELSE 0 END)试一下?
    =====
    select *,(case  when datediff(YEAR,birthdate,getdate())+ 
    (CASE WHEN (DATEADD(YEAR,datediff(YEAR,birthdate,getdate()),birthdate))>GETDATE() THEN 1 ELSE 0 END)<10 then '10岁以下' when datediff(YEAR,birthdate,getdate())+ 
    (CASE WHEN (DATEADD(YEAR,datediff(YEAR,birthdate,getdate()),birthdate))>GETDATE() THEN 1 ELSE 0 END) between 10 and 14 then '10-14岁' when datediff(YEAR,birthdate,getdate())+ 
    (CASE WHEN (DATEADD(YEAR,datediff(YEAR,birthdate,getdate()),birthdate))>GETDATE() THEN 1 ELSE 0 END) between 15 and 19 then '15-19岁' when datediff(YEAR,birthdate,getdate())+ 
    (CASE WHEN (DATEADD(YEAR,datediff(YEAR,birthdate,getdate()),birthdate))>GETDATE() THEN 1 ELSE 0 END) between 20 and 24 then '20-24岁' when datediff(YEAR,birthdate,getdate())+ 
    (CASE WHEN (DATEADD(YEAR,datediff(YEAR,birthdate,getdate()),birthdate))>GETDATE() THEN 1 ELSE 0 END) between 25 and 29 then '25-29岁' else '30岁以上' end) feleifrom tb order by birthdate desc
      

  5.   

    应该是:datediff(YEAR,birthdate,getdate())+ 
    (CASE WHEN (DATEADD(YEAR,datediff(YEAR,birthdate,getdate()),birthdate))>GETDATE() THEN 0 ELSE 1 END)
      

  6.   

    1.你不会先加1再除5?
    2.好象是set datefirst 具体看帮助