select patientid, (case when datediff(yy,birthday,getdate())<=30 then '30以下' when datediff(yy,birthday,getdate())<=40 then '31-40' when datediff(yy,birthday,getdate())<=50 then '41-50' when datediff(yy,birthday,getdate())<=60 then '51-60' else '61以上' end)年龄 from paitient order by 2
create table patient (patientid int , birthday datetime, illtypeid int)create table illtype ( illtypeid int, illtypename nvarchar(50) )insert into patient select 1,'2000-01-01',1 union all select 2,'2002-01-01',2 union all select 3,'1982-01-01',2 union all select 4,'1975-01-01',2 union all select 5,'1956-01-01',3insert into illtype select 1,N'aaaa' union all select 2,N'bbbb' union all select 3,N'cccc'SELECT a.patientid, CASE WHEN DATEDIFF(YEAR, a.birthday, GETDATE()) <= 30 THEN '30以下' WHEN DATEDIFF(YEAR, a.birthday, GETDATE()) <= 40 THEN '31-40' WHEN DATEDIFF(YEAR, a.birthday, GETDATE()) <= 50 THEN '41-50' WHEN DATEDIFF(YEAR, a.birthday, GETDATE()) <= 60 THEN '51-60' ELSE '61以上' END 年龄, a.illtypeid, b.illtypename FROM patient a LEFT JOIN illtype b ON b.illtypeid = a.illtypeid
select patientid, (case when datediff(yy,birthday,getdate())<=30 then '30以下' when datediff(yy,birthday,getdate()) between 31 and 40 then '31-40' when datediff(yy,birthday,getdate()) between 41 and 50 then '41-50' when datediff(yy,birthday,getdate()) between 51 and 60 then '51-60' else '61以上' end)年龄 from paitient order by 2
老大们都不用between ...and 的么?
select ,年龄段=case when datediff(yy,birthday,getdate()) between 31 and 40 then '31-40' when datediff(yy,birthday,getdate()) between 41 and 50 then '41-50' when datediff(yy,birthday,getdate()) between 51 and 60 then '51-60' when datediff(yy,birthday,getdate()) > 60 then '60以上' end, patientid, birthday, illtypename from paitient a inner join illtype b on a.illtypeid=b.illtypeid where datediff(yy,birthday,getdate())>30 order by 年龄段
(case when datediff(yy,birthday,getdate())<=30 then '30以下'
when datediff(yy,birthday,getdate())<=40 then '31-40'
when datediff(yy,birthday,getdate())<=50 then '41-50'
when datediff(yy,birthday,getdate())<=60 then '51-60'
else '61以上'
end)年龄
from paitient
order by 2
create table patient
(patientid int ,
birthday datetime,
illtypeid int)create table illtype
(
illtypeid int,
illtypename nvarchar(50)
)insert into patient
select 1,'2000-01-01',1 union all
select 2,'2002-01-01',2 union all
select 3,'1982-01-01',2 union all
select 4,'1975-01-01',2 union all
select 5,'1956-01-01',3insert into illtype
select 1,N'aaaa' union all
select 2,N'bbbb' union all
select 3,N'cccc'SELECT a.patientid,
CASE
WHEN DATEDIFF(YEAR, a.birthday, GETDATE()) <= 30 THEN '30以下'
WHEN DATEDIFF(YEAR, a.birthday, GETDATE()) <= 40 THEN '31-40'
WHEN DATEDIFF(YEAR, a.birthday, GETDATE()) <= 50 THEN '41-50'
WHEN DATEDIFF(YEAR, a.birthday, GETDATE()) <= 60 THEN '51-60'
ELSE '61以上'
END 年龄,
a.illtypeid,
b.illtypename
FROM patient a
LEFT JOIN illtype b
ON b.illtypeid = a.illtypeid
(case when datediff(yy,birthday,getdate())<=30 then '30以下'
when datediff(yy,birthday,getdate()) between 31 and 40 then '31-40'
when datediff(yy,birthday,getdate()) between 41 and 50 then '41-50'
when datediff(yy,birthday,getdate()) between 51 and 60 then '51-60'
else '61以上'
end)年龄
from paitient
order by 2
select ,年龄段=case when datediff(yy,birthday,getdate()) between 31 and 40 then '31-40'
when datediff(yy,birthday,getdate()) between 41 and 50 then '41-50'
when datediff(yy,birthday,getdate()) between 51 and 60 then '51-60'
when datediff(yy,birthday,getdate()) > 60 then '60以上'
end,
patientid,
birthday,
illtypename
from paitient a inner join illtype b on a.illtypeid=b.illtypeid
where datediff(yy,birthday,getdate())>30 order by 年龄段