使用group by在sql中聚合日期时间

Ham*_*han 4 sql group-by sql-server-2005 aggregate-functions

以下是SQL的可用聚合函数

AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum
Run Code Online (Sandbox Code Playgroud)

我需要在datetime字段上应用聚合函数吗?它没有列在那里.Max(),Min()不起作用.我需要的是

  • 返回最新日期
  • 返回最早的日期

可能吗.我可以以某种方式实现它吗?

SQL*_*ace 8

min()和max()适用于日期

你也可以

最新

select top 1 *
from Table
order by SomeDate desc
Run Code Online (Sandbox Code Playgroud)

最早

select top 1 *
from Table
order by SomeDate 
Run Code Online (Sandbox Code Playgroud)

BTW SQL Server没有first()和last()函数