select year(入职日期),
  ,case when month(入职日期) between 1 and 3 then Q1 
            when month(入职日期) between 4 and 6 then Q2 
            when month(入职日期) between 7 and 9 then Q3 
            when month(入职日期) between 9 and 12 then Q4
  end  季度
 ,avg(datediff(year,getdate(),入职日期)) 平均年龄
from A
group by year(入职日期),
  ,case when month(入职日期) between 1 and 3 then Q1 
            when month(入职日期) between 4 and 6 then Q2 
            when month(入职日期) between 7 and 9 then Q3 
            when month(入职日期) between 9 and 12 then Q4
  end

解决方案 »

  1.   

    select cast(datepart(year,入职日期) as varchar(4))+'Q'+cast(datepart(qq, 入职日期) as char) 季度, 
    avg(datediff(year,生日,入职日期)) 平均年龄 from 表A
    group by cast(datepart(year,入职日期) as varchar(4))+'Q'+cast(datepart(qq, 入职日期) as char)
      

  2.   

    create view v1 as
    select cast(datepart(year,入职日期) as varchar(4))+'Q'+cast(datepart(qq, 入职日期) as char) 季度, 
    avg(datediff(year,生日,入职日期)) 平均年龄 from 表A
    group by cast(datepart(year,入职日期) as varchar(4))+'Q'+cast(datepart(qq, 入职日期) as char)
      

  3.   

    select year(入职日期),
      ,case when month(入职日期) between 1 and 3 then Q1 
                when month(入职日期) between 4 and 6 then Q2 
                when month(入职日期) between 7 and 9 then Q3 
                when month(入职日期) between 9 and 12 then Q4
      end  季度
     ,avg(datediff(year,getdate(),入职日期)) 平均年龄
    from A
    group by year(入职日期),
      ,case when month(入职日期) between 1 and 3 then Q1 
                when month(入职日期) between 4 and 6 then Q2 
                when month(入职日期) between 7 and 9 then Q3 
                when month(入职日期) between 9 and 12 then Q4
      end
      

  4.   

    更正一下:
    select year(入职日期),
      ,case when month(入职日期) between 1 and 3 then Q1 
                when month(入职日期) between 4 and 6 then Q2 
                when month(入职日期) between 7 and 9 then Q3 
                when month(入职日期) between 9 and 12 then Q4
      end  季度
     ,avg(datediff(year,入职日期,getdate())) 平均年龄
    from A
    group by year(入职日期),
      ,case when month(入职日期) between 1 and 3 then Q1 
                when month(入职日期) between 4 and 6 then Q2 
                when month(入职日期) between 7 and 9 then Q3 
                when month(入职日期) between 9 and 12 then Q4
      end