本文介紹了不能使用最小值、最大值等SQL Server 2012的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有這樣的查詢:
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
這篇關于不能使用最小值、最大值等SQL Server 2012的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,