Transact-SQL 是否具有与 MS Logparser Quantize 类似的功能?

发布于 2024-09-26 13:56:27 字数 315 浏览 4 评论 0原文

如果您熟悉 Microsoft Log Parser,您可能会认识 Quantize 函数,该函数会将一个值截断为另一个值的最接近倍数。将日期时间字段分组为增量非常方便。

Date-Time              Count
1/1/2010 00:00         100
1/1/2010 00:15         134
1/1/2010 00:30         56
....

我试图在 Transaction-SQL(特别是 SQL Server 2005 或 2008)中找到类似的函数,它允许我对日期时间进行类似的分组。

If you are familiar with Microsoft Log Parser you probably recognize the Quantize function which will truncate a value to the nearest multiple of another value. It is quite handy for grouping date-time fields into increments.

Date-Time              Count
1/1/2010 00:00         100
1/1/2010 00:15         134
1/1/2010 00:30         56
....

I'm trying to find a similar function in Transaction-SQL (specifically SQL Server 2005 or 2008) that will allow me to do a similar grouping on date-time.

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

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

发布评论

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

评论(2

↘紸啶 2024-10-03 13:56:27

您可以四舍五入到任何给定的分钟数,如下所示:

DateAdd(Minute, (DateDiff(minute, 0, getutcdate() )/15) * 15, 0)

您可以使用日期列、变量或表达式,而不是使用 getutcdate() 。另外,分钟数可以是变量。

declare @minutesQuantize int set @minutesQuantize = 15
DateAdd(Minute, (DateDiff(minute, 0, getutcdate() )/@minutesQuantize) * @minutesQuantize, 0)

唯一的规则是日期差异必须符合整数,即小于 20 亿。这意味着如果没有更复杂的表达式,您就无法计算秒或毫秒。

如果您需要秒或毫秒,请执行以下操作:

dateadd(ms, (datediff(ms, dateadd(day, datediff(day, 0, @date), 0), @date)/@msInterval)*@msInterval, dateadd(day, datediff(day, 0, @date), 0))

或者,如果您想将其包装到一个函数中:

create function dbo.DateRoundMinutes(@dt datetime, @minutes int)
returns datetime
as  begin
return  DateAdd(Minute, (DateDiff(minute, 0, @dt )/@minutes) * @minutes, 0)
end

go
create function dbo.DateRoundMilliseconds(@dt datetime, @ms int)
returns datetime
as begin
 return dateadd(ms, (datediff(ms, dateadd(day, datediff(day, 0, @dt), 0), @dt)/@ms)*@ms, dateadd(day, datediff(day, 0, @dt), 0))
end

您可以这样使用:

select t.dt, 
dbo.DateRoundMilliseconds(dt, 500) dt0_5Second, -- Half second
dbo.DateRoundMilliseconds(dt, 5000) dt5second,  -- 5 second
dbo.DateRoundMilliseconds(dt, 15000) dt15Second,
dbo.DateRoundMilliseconds(dt, 90000) dt90Second,
dbo.DateRoundMinutes(dt, 2) dt2Minute,
dbo.DateRoundMinutes(dt, 5) dt5Minute,
dbo.DateRoundMinutes(dt, 15) dt15Minute,
dbo.DateRoundMinutes(dt, 90) dt90Minute
from
        /* some table having a column dt */

You can round to any given number of minutes like so:

DateAdd(Minute, (DateDiff(minute, 0, getutcdate() )/15) * 15, 0)

Instead of using getutcdate() you can use your date column, variable or expression. In addition the number of minutes can be a variable.

declare @minutesQuantize int set @minutesQuantize = 15
DateAdd(Minute, (DateDiff(minute, 0, getutcdate() )/@minutesQuantize) * @minutesQuantize, 0)

The only rule is that the date difference must fit into an integer, I.e. be less than 2 billion. That means you can't do seconds or milliseconds without a more complicated expression.

If you need seconds or milliseconds do this:

dateadd(ms, (datediff(ms, dateadd(day, datediff(day, 0, @date), 0), @date)/@msInterval)*@msInterval, dateadd(day, datediff(day, 0, @date), 0))

Or, if you want to wrap this into a function:

create function dbo.DateRoundMinutes(@dt datetime, @minutes int)
returns datetime
as  begin
return  DateAdd(Minute, (DateDiff(minute, 0, @dt )/@minutes) * @minutes, 0)
end

go
create function dbo.DateRoundMilliseconds(@dt datetime, @ms int)
returns datetime
as begin
 return dateadd(ms, (datediff(ms, dateadd(day, datediff(day, 0, @dt), 0), @dt)/@ms)*@ms, dateadd(day, datediff(day, 0, @dt), 0))
end

Which you can use like this:

select t.dt, 
dbo.DateRoundMilliseconds(dt, 500) dt0_5Second, -- Half second
dbo.DateRoundMilliseconds(dt, 5000) dt5second,  -- 5 second
dbo.DateRoundMilliseconds(dt, 15000) dt15Second,
dbo.DateRoundMilliseconds(dt, 90000) dt90Second,
dbo.DateRoundMinutes(dt, 2) dt2Minute,
dbo.DateRoundMinutes(dt, 5) dt5Minute,
dbo.DateRoundMinutes(dt, 15) dt15Minute,
dbo.DateRoundMinutes(dt, 90) dt90Minute
from
        /* some table having a column dt */
幸福%小乖 2024-10-03 13:56:27

不直接,事实并非如此。但是您可以按一个函数(您编写的)进行分组,该函数将日期时间列四舍五入到最接近的一刻钟(或 Quantize 所做的任何事情)。

SELECT
    dbo.QuarterHour(DateColumn) AS Date-Time
  , COUNT(*) AS Count
FROM MyTable
GROUP BY dbo.QuarterHour(DateColumn)

Not directly, it doesn't. But you can group by a function (that you write) that rounds the datetime column to its nearest quarter-hour (or whatever Quantize does).

SELECT
    dbo.QuarterHour(DateColumn) AS Date-Time
  , COUNT(*) AS Count
FROM MyTable
GROUP BY dbo.QuarterHour(DateColumn)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文