SQL - 将开始/结束时间分成 15 分钟记录
我有一个记录集,其中包含两个单独字段中的开始时间和结束时间:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不用服务器端语言进行处理呢?这会容易得多,并且肯定会让您保持数据库中立。
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.
似乎有两种基本方法。
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.
我同意 Keith 的观点,即这可能最好在应用程序中完成。对于大型源表来说,服务器和到查找表的交叉连接会变得昂贵,但为了好玩,我制作了一个快速示例。
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.