如何在 SQL Server 2000 中返回基于连续时间分配的记录列表
我有一个如下所示的表:
IP Hostname TransactionDate
------------- ---------- -------------------
1.1.1.1 A 2009-01-01 01:00:00
1.1.1.1 A 2009-01-02 01:00:00
1.1.1.1 A 2009-01-03 01:45:00
1.1.1.1 B 2009-01-04 01:00:00
1.1.1.1 A 2009-01-05 01:00:00
我想构建一个查询来返回记录,该记录将根据连续持有 IP 地址的时间对结果进行分组:
即(选择或 IP 1.1.1.1):
Hostname GrantDate ExpireDate
----------- --------------- -----------------
A 2009-01-01 01:00:00 2009-01-04 01:00:00
B 2009-01-04 01:00:00 2009-01-05 01:00:00
A 2009-01-05 01:00:00 NULL
最好的方法是什么完成这个任务? 我想尽可能避免使用光标。 我正在使用 SQL Server 2000,这使得这变得更加困难......
I have a Table that looks like this:
IP Hostname TransactionDate
------------- ---------- -------------------
1.1.1.1 A 2009-01-01 01:00:00
1.1.1.1 A 2009-01-02 01:00:00
1.1.1.1 A 2009-01-03 01:45:00
1.1.1.1 B 2009-01-04 01:00:00
1.1.1.1 A 2009-01-05 01:00:00
I would like to build a query to return records that will group results based on how long they've contiguously held an IP address:
ie (selecting or IP 1.1.1.1):
Hostname GrantDate ExpireDate
----------- --------------- -----------------
A 2009-01-01 01:00:00 2009-01-04 01:00:00
B 2009-01-04 01:00:00 2009-01-05 01:00:00
A 2009-01-05 01:00:00 NULL
What's the best way to accomplish this task? I'd like to avoid cursors where possible. I'm using SQL Server 2000 which makes this more difficult...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Joe Celko 的 SQL 谜题和答案中提供了一些针对此问题的解决方案。 Google 有 摘录,其中包括它们。 他的示例包括开始时间和结束时间,但调整它们应该相当简单。
There are a few solutions to this problem in Joe Celko's SQL Puzzles and Answers. Google has an excerpt which includes them. His examples include start and end times, but it should be fairly simple to adapt them.
为了后代的缘故而发布。 我只是找不到一种不使用光标的方法来做到这一点。 我确信,部分原因是 SQL 不是我的强项。 如果其他人看到这篇文章,这是我使用的解决方案。 我将继续努力使用查询来查询它,如果我弄清楚了,我会发布它。
结果:
Posted for Posterity's sake. I just can't find a way to do this without using a cursor. Part of it, I'm sure, is that SQL is not my strong point. If anyone else runs across this post, here's the solution I've used. I'm going to keep plugging away at using a query for it and will post that if I ever figure it out.
Results: