create table student (
sno int,
birth datetime,
name varchar(50))
计算最大年龄和最小年龄的差别。
我写的如下:
select max(age)-min(age) from (select DATEDIFF(year,birth,getdate()) as age from student)
怎么出错了?
sno int,
birth datetime,
name varchar(50))
计算最大年龄和最小年龄的差别。
我写的如下:
select max(age)-min(age) from (select DATEDIFF(year,birth,getdate()) as age from student)
怎么出错了?
select max(age)-min(age) from (select DATEDIFF(year,birth,getdate()) as age from student) as s_age
create table #student (
sno int,
birth datetime,
name varchar(50))
insert #student
select 1,'1980-01-01','a' union all
select 2,'1981-11-01','b' union all
select 3,'1982-01-01','c' union all
select 4,'1983-11-01','d' union all
select 5,'1984-01-01','e' union all
select 6,'1985-12-01','f' union all
select 7,'1986-08-01','g'
select *
from #student
select DATEDIFF(year ,MIN(t.birth),MAX(t.birth)) as 'YearDiff'
from #student t
drop table #student
sno int,
birth datetime,
name varchar(50))
select datediff(dd,min(birth),max(birth)) from student
生日的差别就是年龄的差别啊,只要处理生日的时间差就行了!
select datediff(yy,min(birth),max(birth)) from student