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)
怎么出错了?

解决方案 »

  1.   

    为子查询增加一个别名就可以了,如下:
    select max(age)-min(age) from (select DATEDIFF(year,birth,getdate()) as age from student) as s_age
      

  2.   


    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
      

  3.   

    差个别名select max(age)-min(age) from (select DATEDIFF(year,birth,getdate()) as age from student) t
      

  4.   

    别名  select * from (select * from TB)t
      

  5.   

    注意SQL语句的出错信息,细心检查解决此类问题应该没多大困难!
      

  6.   

    create table student (
    sno int,
    birth datetime,
    name varchar(50))
    select datediff(dd,min(birth),max(birth)) from student
    生日的差别就是年龄的差别啊,只要处理生日的时间差就行了!
      

  7.   


    select datediff(yy,min(birth),max(birth)) from student