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

标签 sql sql-server-2005 group-by aggregate-functions

以下是可用的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

我需要在日期时间字段上应用聚合函数吗?它没有在那里列出。 Max(), Min() 将不起作用。我需要的是
  • 返回最新日期
  • 返回最早日期

  • 是否可以。我可以以某种方式实现它吗?

    最佳答案

    min() 和 max() 可以很好地处理日期

    你也可以这样做

    最新的

    select top 1 *
    from Table
    order by SomeDate desc
    

    最早的
    select top 1 *
    from Table
    order by SomeDate 
    

    顺便说一句,SQL Server 没有 first() 和 last() 函数

    关于sql - 使用 group by 聚合 sql 中的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8360667/

    相关文章:

    php - 检查数据库表是否存在行,如果不存在则创建它

    mysql - 如何提高 SQL 计数性能?

    sql - Microsoft SQL Server 2005 中的默认隔离级别

    sql-server - 输出列不在目标表中?

    mysql - SQL 按日期分组,但也获取没有记录的日期

    pandas - 在两个 pandas 数据帧之间分配值

    mysql - PostgreSQL:如何使用 GROUPING SETS、CUBE 和 ROLLUP 进行汇总

    mysql - 使用删除 SQL 查询的限制

    sql-server-2005 - 检查 SQL 服务器是否以编程方式可用?

    sql - T-SQL : How to select rows based on the max date?