如何在 SQL Server 2000 中返回基于连续时间分配的记录列表

发布于 2024-07-13 15:01:11 字数 777 浏览 3 评论 0原文

我有一个如下所示的表:

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 技术交流群。

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

发布评论

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

评论(2

那小子欠揍 2024-07-20 15:01:11

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.

仙女 2024-07-20 15:01:11

为了后代的缘故而发布。 我只是找不到一种不使用光标的方法来做到这一点。 我确信,部分原因是 SQL 不是我的强项。 如果其他人看到这篇文章,这是我使用的解决方案。 我将继续努力使用查询来查询它,如果我弄清楚了,我会发布它。

SET NOCOUNT ON
DECLARE @ip VARCHAR(15)
    SET @ip = '1.1.1.1'

DECLARE @dhcplog TABLE(IP VARCHAR(15), HOSTNAME VARCHAR(32), IPDATE DATETIME)
DECLARE @results TABLE(IP VARCHAR(15), HOSTNAME VARCHAR(32), STARTDATE DATETIME, ENDDATE DATETIME)
INSERT INTO @dhcplog VALUES('1.1.1.1', 'A', '2009-01-01 01:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'A', '2009-01-02 02:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'A', '2009-01-05 03:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'B', '2009-01-07 04:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'B', '2009-01-07 10:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'B', '2009-01-08 05:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'C', '2009-01-09 06:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'A', '2009-01-10 07:00:00')

DECLARE @cHOST VARCHAR(32)
DECLARE @cEND DATETIME

DECLARE @tIP VARCHAR(15)
DECLARE @tHOST VARCHAR(32)
DECLARE @tIPDATE DATETIME

DECLARE IPCursor CURSOR FOR SELECT IP, HOSTNAME, IPDATE
                            FROM @dhcplog
                            WHERE IP = @ip
                            GROUP BY IP, HOSTNAME, IPDATE
                            ORDER BY IPDATE DESC

OPEN IPCursor
    FETCH NEXT FROM IPCursor INTO @tIP, @tHOST, @tIPDATE
        WHILE @@FETCH_STATUS = 0
            BEGIN
                IF @tHOST = @cHOST
                    BEGIN
                        UPDATE @results
                        SET STARTDATE = @tIPDATE
                        WHERE HOSTNAME = @cHOST
                        AND ENDDATE = @cEND
                    END
                ELSE
                    BEGIN
                        INSERT INTO @results (IP, HOSTNAME, STARTDATE, ENDDATE)
                        VALUES (@tIP, @tHOST, @tIPDATE, @tIPDATE)
                        SET @cHOST = @tHOST
                        SET @cEND = @tIPDATE
                    END
                FETCH NEXT FROM IPCursor INTO @tIP, @tHOST, @tIPDATE
            END
CLOSE IPCursor
DEALLOCATE IPCursor

SELECT * FROM @results

SET NOCOUNT OFF

结果:

IP      Hostname    StartDate       EndDate
1.1.1.1 A       2009-01-10 07:00:00 2009-01-10 07:00:00
1.1.1.1 C       2009-01-09 06:00:00 2009-01-09 06:00:00
1.1.1.1 B       2009-01-07 04:00:00 2009-01-08 05:00:00
1.1.1.1 A       2009-01-01 01:00:00 2009-01-05 03:00:00

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.

SET NOCOUNT ON
DECLARE @ip VARCHAR(15)
    SET @ip = '1.1.1.1'

DECLARE @dhcplog TABLE(IP VARCHAR(15), HOSTNAME VARCHAR(32), IPDATE DATETIME)
DECLARE @results TABLE(IP VARCHAR(15), HOSTNAME VARCHAR(32), STARTDATE DATETIME, ENDDATE DATETIME)
INSERT INTO @dhcplog VALUES('1.1.1.1', 'A', '2009-01-01 01:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'A', '2009-01-02 02:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'A', '2009-01-05 03:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'B', '2009-01-07 04:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'B', '2009-01-07 10:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'B', '2009-01-08 05:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'C', '2009-01-09 06:00:00')
INSERT INTO @dhcplog VALUES('1.1.1.1', 'A', '2009-01-10 07:00:00')

DECLARE @cHOST VARCHAR(32)
DECLARE @cEND DATETIME

DECLARE @tIP VARCHAR(15)
DECLARE @tHOST VARCHAR(32)
DECLARE @tIPDATE DATETIME

DECLARE IPCursor CURSOR FOR SELECT IP, HOSTNAME, IPDATE
                            FROM @dhcplog
                            WHERE IP = @ip
                            GROUP BY IP, HOSTNAME, IPDATE
                            ORDER BY IPDATE DESC

OPEN IPCursor
    FETCH NEXT FROM IPCursor INTO @tIP, @tHOST, @tIPDATE
        WHILE @@FETCH_STATUS = 0
            BEGIN
                IF @tHOST = @cHOST
                    BEGIN
                        UPDATE @results
                        SET STARTDATE = @tIPDATE
                        WHERE HOSTNAME = @cHOST
                        AND ENDDATE = @cEND
                    END
                ELSE
                    BEGIN
                        INSERT INTO @results (IP, HOSTNAME, STARTDATE, ENDDATE)
                        VALUES (@tIP, @tHOST, @tIPDATE, @tIPDATE)
                        SET @cHOST = @tHOST
                        SET @cEND = @tIPDATE
                    END
                FETCH NEXT FROM IPCursor INTO @tIP, @tHOST, @tIPDATE
            END
CLOSE IPCursor
DEALLOCATE IPCursor

SELECT * FROM @results

SET NOCOUNT OFF

Results:

IP      Hostname    StartDate       EndDate
1.1.1.1 A       2009-01-10 07:00:00 2009-01-10 07:00:00
1.1.1.1 C       2009-01-09 06:00:00 2009-01-09 06:00:00
1.1.1.1 B       2009-01-07 04:00:00 2009-01-08 05:00:00
1.1.1.1 A       2009-01-01 01:00:00 2009-01-05 03:00:00
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文