Select Top problem

  • CREATE TABLE ClientRequests

    (

    Id INT IDENTITY PRIMARY KEY,

    Ip VARCHAR(200) NOT NULL,

    Requests INT NOT NULL

    )

    GO

    INSERT INTO ClientRequests (Ip, Requests)

    VALUES

    ('1.1.1.10', 5100),

    ('2.2.2.20', 10000),

    ('3.3.3.30', 200),

    ('3.3.3.30', 44000),

    ('4.4.4.40', 2200),

    ('5.5.5.50', 10000),

    ('6.6.6.60', 31000),

    ('7.7.7.70', 100),

    ('8.8.8.80', 300),

    ('9.9.9.90', 10000)

    GO

    Now run this Query

    SELECT TOP (3) *

    FROM ClientRequests

    ORDER BY Requests DESC

    When running above query for top(3) it will not return the 5.5.5.50 and 9.9.9.90 which have also same requests

  • any good....

    SELECT TOP (3) WITH TIES *

    FROM ClientRequests

    ORDER BY Requests DESC

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • SELECT TOP (3) WITH TIES *

    FROM ClientRequests

    ORDER BY Requests DESC


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • I think you're looking for this:

    SELECT Id, IP, Requests

    FROM (

    SELECT *, RNK = DENSE_RANK() OVER(ORDER BY Requests DESC)

    FROM ClientRequests

    ) AS data

    WHERE RNK <= 3

    -- Gianluca Sartori

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply