我有一张表'Students'
,其中包含'DateOfBirth'
名学生。我需要SQL statement
我可以在查询中使用average
和minimum ages
作为带有这些名称的字段吗?我不想要添加年龄字段我只想要一个可以复制和粘贴的SQL语句,它将返回我需要的内容。
SELECT MIN(DateOfBirth) AS Min Age, AVG(DateOfBirth) AS Avg Age FROM Students;
目前我发现的所有建议都要求我在运行时指定一个值,我不明白为什么?
答案 0 :(得分:3)
您应首先从date of birth
计算年龄,然后找出平均年龄
Select AVG(Datediff("yyyy",DateOfBirth,getdate())) as AVGage from Students
Select MIN(Datediff("yyyy",DateOfBirth,getdate())) as MINage from Students
你也可以在一个查询中对avg,min进行calucate
Select AVG(Datediff("yyyy",DateOfBirth,getdate())) as AVGage ,
MIN(Datediff("yyyy",DateOfBirth,getdate())) as MINage
from Students
FOR MS ACCESS DB:
您可以使用日期()函数
Select AVG(Datediff("yyyy",DateOfBirth,DATE())) as AVGage ,
MIN(Datediff("yyyy",DateOfBirth,DATE())) as MINage
from Students
答案 1 :(得分:0)
SELECT *
FROM Students
GROUP BY DateOfBirth
ORDER BY DateOfBirth DESC
SELECT MIN(DateOfBirth) AS MinAges
FROM Students
SELECT AVG(DateOfBirth) AS AverageAges
FROM Students
答案 2 :(得分:0)
使用聚合函数AVG和MIN ..你可以轻松谷歌它btw ...要计算年龄你可以使用getDate函数和计算年龄 - 取决于你的格式DateOfBirth列..
select AVG((getDate()-DateOfBirth)/365) as avgAge ,
MIN ((getDate()-DateOfBirth)/365) as minAge
from Students