如何只获取查询结果的第二、第三、第四或第五行?(SQL SERVER 2000)

发布于 2024-07-13 11:01:25 字数 423 浏览 5 评论 0原文

我需要一个建议,甚至一个解决方案,如何以高性能获取查询结果的第二、第三...行。 我知道有可能 row_number(SQL Server 2005 或更高版本),但不适用于 SQL Server 2000 :-(

我第一次尝试获取第五行是:
选择前 1 列
FROM(选择前 5 列
来自表
按 col) q
排序 ORDER BY col DESC

我尝试解释我的要求:在我的表中最多可以有。 一个人可以排5排。 但如果这个人只存在 4 行,我上面的查询就会得到错误的结果。

有什么建议么?

任何帮助将不胜感激!

谢谢转发,最好的问候安德烈亚斯

I would need a suggestion or even a solution, how to get only the second,third... row of a query result with high performance. I know there is the possibility of row_number(SQL Server 2005 or higher) but not for SQL Server 2000 :-(

My first try to get the fifth row was:
SELECT TOP 1 col
FROM (SELECT TOP 5 col
FROM table
ORDER BY col) q
ORDER BY col DESC

I try to explain my requirements: In my table there can be max. 5 rows for one person. But if only 4 rows for this person exists I would get a wrong result with my above query.

Any suggestions?

Any help will be appreciated!

Thx forward, Best Regards Andreas

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

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

发布评论

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

评论(5

蘑菇王子 2024-07-20 11:01:25

也许最好、最快的方法就是在数据读取器中选择前 5 个,然后根据需要将下一个读入内存。

Probably the best and fastest way to do this is just select the top 5 in a datareader, and just read the next into memory as you need it.

夏末的微笑 2024-07-20 11:01:25

要获取第 5 行,您可以使用多个子查询,例如:

select top 1 MyCol from MyTable
where MyCol = 
(
    select top 1 MyCol from 
    (
        select top 5 MyCol from MyTable order by MyCol
    ) t order by MyCol desc
)

To get the 5th Row, you can use multiple subqueries like:

select top 1 MyCol from MyTable
where MyCol = 
(
    select top 1 MyCol from 
    (
        select top 5 MyCol from MyTable order by MyCol
    ) t order by MyCol desc
)
梦里寻她 2024-07-20 11:01:25

这应该可以做到...

CREATE PROCEDURE [dbo].[sp_UpdateSkills]
AS
BEGIN
    set nocount on

    declare @UserID int
    declare @Skill varchar(1)
    declare @SkillCount int

    declare @Skill1 varchar(1)
    declare @Skill2 varchar(1)
    declare @Skill3 varchar(1)
    declare @Skill4 varchar(1)
    declare @Skill5 varchar(1)

    declare csrUser cursor for
        select distinct UserID
        from dbo.tblSkills1
        order by 1

    open csrUser

    fetch next from csrUser into @UserID
    while (@@fetch_status = 0)
    begin
        declare csrSkill cursor for
            select Skill
            from dbo.tblSkills1
            where UserID = @UserID
            order by Skill

        set @SkillCount = 1
        set @Skill1 = null
        set @Skill2 = null
        set @Skill3 = null
        set @Skill4 = null
        set @Skill5 = null

        open csrSkill

        fetch next from csrSkill into @Skill
        while (@@fetch_status = 0 and @SkillCount < 6)
        begin
            print @UserID
            print @Skill

            if (@SkillCount = 1)
                set @Skill1 = @Skill
            else if (@SkillCount = 2)
                set @Skill2 = @Skill
            else if (@SkillCount = 3)
                set @Skill3 = @Skill
            else if (@SkillCount = 4)
                set @Skill4 = @Skill
            else if (@SkillCount = 5)
                set @Skill5 = @Skill

            set @SkillCount = @SkillCount + 1

            fetch next from csrSkill into @Skill
        end

        close csrSkill
        deallocate csrSkill

        insert into tblSkills2
        (UserID, Skill1, Skill2, Skill3, Skill4, Skill5)
        values
        (@UserID, @Skill1, @Skill2, @Skill3, @Skill4, @Skill5)

        fetch next from csrUser into @UserID
    end

    close csrUser
    deallocate csrUser
END

This should do it...

CREATE PROCEDURE [dbo].[sp_UpdateSkills]
AS
BEGIN
    set nocount on

    declare @UserID int
    declare @Skill varchar(1)
    declare @SkillCount int

    declare @Skill1 varchar(1)
    declare @Skill2 varchar(1)
    declare @Skill3 varchar(1)
    declare @Skill4 varchar(1)
    declare @Skill5 varchar(1)

    declare csrUser cursor for
        select distinct UserID
        from dbo.tblSkills1
        order by 1

    open csrUser

    fetch next from csrUser into @UserID
    while (@@fetch_status = 0)
    begin
        declare csrSkill cursor for
            select Skill
            from dbo.tblSkills1
            where UserID = @UserID
            order by Skill

        set @SkillCount = 1
        set @Skill1 = null
        set @Skill2 = null
        set @Skill3 = null
        set @Skill4 = null
        set @Skill5 = null

        open csrSkill

        fetch next from csrSkill into @Skill
        while (@@fetch_status = 0 and @SkillCount < 6)
        begin
            print @UserID
            print @Skill

            if (@SkillCount = 1)
                set @Skill1 = @Skill
            else if (@SkillCount = 2)
                set @Skill2 = @Skill
            else if (@SkillCount = 3)
                set @Skill3 = @Skill
            else if (@SkillCount = 4)
                set @Skill4 = @Skill
            else if (@SkillCount = 5)
                set @Skill5 = @Skill

            set @SkillCount = @SkillCount + 1

            fetch next from csrSkill into @Skill
        end

        close csrSkill
        deallocate csrSkill

        insert into tblSkills2
        (UserID, Skill1, Skill2, Skill3, Skill4, Skill5)
        values
        (@UserID, @Skill1, @Skill2, @Skill3, @Skill4, @Skill5)

        fetch next from csrUser into @UserID
    end

    close csrUser
    deallocate csrUser
END
×纯※雪 2024-07-20 11:01:25

如果我理解正确的话,例如,您只希望在实际上有第五行时返回该行。 在这种情况下,您可以这样做:

SELECT TOP 1 col
FROM (
    SELECT TOP 5 col
    FROM table
    where (SELECT COUNT(*) FROM table) >= 5
    ORDER BY col
) q
ORDER BY col DESC

If I understand correctly, you only want the row returned if there actually is a 5th row, for example. In that case, you can do this:

SELECT TOP 1 col
FROM (
    SELECT TOP 5 col
    FROM table
    where (SELECT COUNT(*) FROM table) >= 5
    ORDER BY col
) q
ORDER BY col DESC
離人涙 2024-07-20 11:01:25

这是另一个例子。 该表使用带有标识列的临时表。

SELECT TOP 5 IDENTITY(int) as id, col
  INTO #t
  FROM table
 ORDER BY col

SELECT col FROM #t WHERE id = 5

DROP TABLE #t

Here's another example. This one uses a temporary table with an identity column.

SELECT TOP 5 IDENTITY(int) as id, col
  INTO #t
  FROM table
 ORDER BY col

SELECT col FROM #t WHERE id = 5

DROP TABLE #t
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文