1

I have to count all the teachers whose ages are going to be 60 at June 2013, June 2014 and June 2015. Details : at 2013 there is one teacher 2014 there is 5 teachers same etc.

I don't know how to get the three next years with June and count the number of teachers whose age will be 60 at June 2013, June 2014 and June 2015.

Here is the table:

TeacherName         DateOfBirth               TeacherAge    june2013    june2014    june2015
S. KHALID SALIM     1958-03-23 00:00:00.000   55            
ZUBAIDA BEGUM       1976-08-01 00:00:00.000   36            
TANZEEM AKHTAR      1974-09-25 00:00:00.000   38            
MARYAM BIBI         1982-08-18 00:00:00.000   30            
RABIA TABASSUM      1976-11-10 00:00:00.000   36    

Here is my query:

SELECT  TeacherName, DateOfBirth,
  datediff(mm, DateOfBirth, getdate()) / 12 as TeacherAge,
  '' as  june2013(countteachers) , '' as june2014(countteachers), '' as june2015(countteachersonlywhoseagewillbe60atjune2015)
FROM Teachers
4

2 回答 2

0

所以你想计算所有将在 2013/2014/2015 年 6 月完成 60 年的教师吗?

这确实是对生日的限制:

select count(*) from teachers where 
    dateofbirth between '1953-06-01' and '1953-06-30 23:59:58'
    OR dateofbirth between '1954-06-01' and '1954-06-30 23:59:58'
    OR dateofbirth between '1955-06-01' and '1955-06-30 23:59:58'
于 2013-05-15T12:44:31.063 回答
0

尝试这个:

DECLARE @dob  DATETIME, @june2013date DATETIME, @june2014date DATETIME
SET @dob='1980-01-09 00:00:00'
SET @june2013date='2013-06-01 00:00:00'
SET @june2014date='2014-06-01 00:00:00'


SELECT DATEDIFF(hour,@dob,GETDATE())/8766  AS Age, 
   DATEDIFF(hour,@dob,@june2013date)/8766 AS June2013Age, 
   DATEDIFF(hour,@dob,@june2014date)/8766 AS June2014Age

它应该返回如下内容:

Age         June2013Age June2014Age
----------- ----------- -----------
33          33          34
于 2013-05-15T12:13:34.103 回答