SQL - 将开始/结束时间分成 15 分钟记录

发布于 2024-09-03 01:12:37 字数 404 浏览 4 评论 0原文

我有一个记录集,其中包含两个单独字段中的开始时间和结束时间:

id - Int
startTime - DateTime
endTime - DateTime

我想找到一种方法来查询记录并根据开始之间找到的 15 分钟间隔数将其作为 X 记录返回和结束时间。

例如,假设我有这样的记录:

id, StartTime, EndTime
1, 1/1/2010 8:28 AM, 1/1/2010 8:47 AM

我将返回 3 条记录,第一条记录代表 8:15 间隔,#2 代表 8:30 间隔,然后第三条记录代表 8:45 间隔。

我意识到这可以使用存储过程中的逻辑来完成,但我们试图保持数据库中立,因为我们支持多个数据库引擎。

I've got a record set that consists of a start and end time in two separate fields:

id - Int
startTime - DateTime
endTime - DateTime

I'd like to find a way to query a record and return it as X records based on the number of 15 minute intervals found between the start and end times.

For example, let's say I have a record like this:

id, StartTime, EndTime
1, 1/1/2010 8:28 AM, 1/1/2010 8:47 AM

I would return 3 records, the first would represent the 8:15 interval, #2 for the 8:30 interval and then a 3rd for the 8:45 interval.

I realize this could be done using logic in an sproc, but we are trying to remain db neutral as we support multiple database engines.

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

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

发布评论

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

评论(3

瞄了个咪的 2024-09-10 01:12:37

为什么不用服务器端语言进行处理呢?这会容易得多,并且肯定会让您保持数据库中立。

Why don't you do the processing in a server-side language? It would be much easier and would definitely allow you to be db-neutral.

渡你暖光 2024-09-10 01:12:37

似乎有两种基本方法。
1. 迭代计算每个区间(循环或递归)
2. 使用查找表

由于某些函数的实现(用于递归计算)甚至循环的定义都会有所不同,因此最通用的似乎是查找表。它也可能会非常快。

There appear to be two basic approaches.
1. Iteratively caclulate each interval (either a loop or recursion)
2. Use a lookup table

As some the implentation of functions (for recursive calculation) and even the defintion for Loops will vary, the most generic would appear to be a lookup table. It is also likely to be very quick.

少女七分熟 2024-09-10 01:12:37

我同意 Keith 的观点,即这可能最好在应用程序中完成。对于大型源表来说,服务器和到查找表的交叉连接会变得昂贵,但为了好玩,我制作了一个快速示例。

   declare @QuarterHours table (
        QuarterHour time
    )

    declare @x time
    set @x = '00:00'

    insert into @QuarterHours
        (QuarterHour)
        values 
        (@x)
    set @x = DATEADD(minute, 15, @x)

    while @x <> '00:00' begin
        insert into @QuarterHours
            (QuarterHour)
            values 
            (@x)
        set @x = DATEADD(minute, 15, @x)
    end /* while */

    declare @test table (
        id int,
        starttime datetime,
        endtime datetime
    )

    insert into @test
        (id, starttime, endtime)
        values
        (1, '2010-01-01 08:28', '2010-01-01 08:47')

    select t.id, q.QuarterHour
        from @test t
            cross join @QuarterHours q
        where q.QuarterHour between cast(t.starttime as time) and cast(t.endtime as time)

I agree with Keith that this is probably better done in an app. server and the cross join to the lookup table will get expensive for a large source table, but just for fun I worked up a quick sample.

   declare @QuarterHours table (
        QuarterHour time
    )

    declare @x time
    set @x = '00:00'

    insert into @QuarterHours
        (QuarterHour)
        values 
        (@x)
    set @x = DATEADD(minute, 15, @x)

    while @x <> '00:00' begin
        insert into @QuarterHours
            (QuarterHour)
            values 
            (@x)
        set @x = DATEADD(minute, 15, @x)
    end /* while */

    declare @test table (
        id int,
        starttime datetime,
        endtime datetime
    )

    insert into @test
        (id, starttime, endtime)
        values
        (1, '2010-01-01 08:28', '2010-01-01 08:47')

    select t.id, q.QuarterHour
        from @test t
            cross join @QuarterHours q
        where q.QuarterHour between cast(t.starttime as time) and cast(t.endtime as time)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文