不能使用最小值、最大值等SQL Server 2012

2022-04-01 00:00:00 aggregate-functions sql-server

我有这样的查询:

 select 
      FirstName, LastName, BirthDate,
      GETDATE() AS [Today],
      CAST (DATEDIFF (DD, BirthDate, GETDATE())/365.25 AS INT) AS [Age]
FROM
      Employees
WHERE CAST (DATEDIFF (DD, BirthDate, GETDATE())/365.25 AS INT) >=55
ORDER BY LastName ASC

我想找到最大、最小和平均的年龄,我认为我可以做到这一点,但显然我做不到。如何才能做到这一点?

 select 
     FirstName, LastName, BirthDate,
     GETDATE() AS [Today],
     CAST (DATEDIFF (DD, BirthDate, GETDATE())/365.25 AS INT) AS [Age],
     MIN (DATEDIFF (DD, BirthDate, GETDATE())/365.25 AS INT)
FROM
     Employees
WHERE 
     CAST (DATEDIFF (DD, BirthDate, GETDATE())/365.25 AS INT) >=55
ORDER BY 
     LastName ASC

错误:

消息195,级别15,州10,第5行
"min"不是可识别的内置函数名。


解决方案

这是我找到的解决方案。感谢大家的帮助!

SELECT
       MAX (CAST (DATEDIFF (DD, BirthDate, GETDATE())/365.25 AS INT)) AS [The Biggest Age]
     , MIN (CAST (DATEDIFF (DD, BirthDate, GETDATE())/365.25 AS INT)) AS [The Smallest Age]
     , AVG (CAST (DATEDIFF (DD, BirthDate, GETDATE())/365.25 AS INT)) AS [The Average Age]
FROM
      Employees

相关文章