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

发布于 2024-12-19 04:05:00 字数 486 浏览 3 评论 0原文

以下是 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() 不起作用。我需要的是

  • 返回最晚日期
  • 返回最早日期

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

The following are available aggregate functions for 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

I need to apply aggregate function on datetime field? It is not listed there. Max(), Min() will not work. What I would need is either

  • return the latest date
  • return the earliest date

Is it possible. Can I implement it somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇俏 2024-12-26 04:05:00

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

您也可以执行

最新

select top 1 *
from Table
order by SomeDate desc

最早的

select top 1 *
from Table
order by SomeDate 

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

min() and max() work fine with dates

you can also do

latest

select top 1 *
from Table
order by SomeDate desc

earliest

select top 1 *
from Table
order by SomeDate 

BTW SQL Server does not have the first() and last() functions

樱花坊 2024-12-26 04:05:00

当然,MINMAX 有效。.

Select 
    Min (MyDate) MinDate, 
    MAX (Mydate) MaxDate, 
    COUNT (Mydate) NumDates
FROM
(
    Select GETDATE() + 5 MyDate
    UNION Select GETDATE()+4
    UNION Select GETDATE()+3
    UNION Select GETDATE()+2
    UNION Select GETDATE()+1
    UNION Select GETDATE()-0
    UNION Select GETDATE()-1
    UNION Select GETDATE()-2
    UNION Select GETDATE()-3
    UNION Select GETDATE()-4
    UNION Select GETDATE()-5
) DateList

将返回

MinDate                 MaxDate                 NumDates
----------------------- ----------------------- -----------
2011-11-27 13:14:47.013 2011-12-07 13:14:47.013 11

(1 row(s) affected)

但是,并非所有聚合都有效。 SUMAVG 不起作用,您将收到消息:

Msg 8117, Level 16, State 1, Line 4
Operand data type datetime is invalid for avg operator.

Of course the MIN and MAX works..

Select 
    Min (MyDate) MinDate, 
    MAX (Mydate) MaxDate, 
    COUNT (Mydate) NumDates
FROM
(
    Select GETDATE() + 5 MyDate
    UNION Select GETDATE()+4
    UNION Select GETDATE()+3
    UNION Select GETDATE()+2
    UNION Select GETDATE()+1
    UNION Select GETDATE()-0
    UNION Select GETDATE()-1
    UNION Select GETDATE()-2
    UNION Select GETDATE()-3
    UNION Select GETDATE()-4
    UNION Select GETDATE()-5
) DateList

will return

MinDate                 MaxDate                 NumDates
----------------------- ----------------------- -----------
2011-11-27 13:14:47.013 2011-12-07 13:14:47.013 11

(1 row(s) affected)

However, not all aggregations work. SUM, AVG do not work and you will get the message:

Msg 8117, Level 16, State 1, Line 4
Operand data type datetime is invalid for avg operator.
柳絮泡泡 2024-12-26 04:05:00

是的,我们可以,如果日期作为表的列存在,就像我们有一个表 tbldate ,其中包含名为 Datecolmn 的列。
然后使用 sql 查询来获取此列的最大值

Select Max(Datecolmn ) from tbldate 

,它会为您提供此表中存在的最大日期......

Yes we can, if the date is present as column of the table, Like we have one table tbldate which contain Column having name Datecolmn .
then use the sql query to get the max value of this column Like

Select Max(Datecolmn ) from tbldate 

and it gives you the max date which is present in this table.....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文