sql >> Database >  >> RDS >> Sqlserver

SQL Server 2005 - Row_Number()

ORDER BY in ROW_NUMBER clausule garandeert niet de volgorde van de resultatenset.

ROW_NUMBER gebruikt meestal sortering in het zoekplan, wat resulteert in het feit dat de waarden voorgesorteerd uitkomen.

Dit is een bijwerking en er mag niet op worden vertrouwd.

DISTINCT gebruikt Hash Match (Aggregate) wat het sorteren verbreekt.

Voeg ORDER BY toe clausule aan het einde van de zoekopdracht:

SELECT  DISTINCT (ID), State_Id, Name_Of_Trip, Date_Of_Travel, Creation_Date, Locking_Id, Applicant_Name, Reference_Number, State_Name
FROM    (
        SELECT  app.ID, app.State_Id, app.Name_Of_Trip, app.Date_Of_Travel,
                app.Creation_Date, app.Locking_Id, app.Applicant_Name, app.Reference_Number, 
                State.Name AS State_Name, ROW_NUMBER() OVER(ORDER BY Reference_Number DESC) as rowNum  
        FROM    Application_Leg AS app 
        INNER JOIN
                State AS state 
        ON      app.State_Id = state.ID
        WHERE   app.State_Id IN (5, 6, 8)
                AND app.Organisation_Id = 12 
                AND Leg_Number IN
                (
                SELECT  Leg_Number 
                FROM    Application_Leg as al
                INNER JOIN
                        Organisation as org
                ON      al.Organisation_Id = org.ID
                WHERE   al.ID = app.ID
                        AND org.Approval_Required = 1
                        AND Mode_Of_Transport = 1
                )
        ) AS pagedApplications
WHERE   rowNum BETWEEN 0 AND (0 + 10)
ORDER BY
        ReferenceNumber DESC

Houd er ook rekening mee dat het 10 niet zal retourneren verschillende resultaten, zal het DISTINCT . teruggeven van de eerste 10 resultaten.

Als je het eerste wilt, gebruik dan dit:

SELECT  DISTINCT TOP 10 ID, State_Id, Name_Of_Trip, Date_Of_Travel, Creation_Date, Locking_Id, Applicant_Name, Reference_Number, State_Name
FROM    (
        SELECT  app.ID, app.State_Id, app.Name_Of_Trip, app.Date_Of_Travel,
                app.Creation_Date, app.Locking_Id, app.Applicant_Name, app.Reference_Number, 
                State.Name AS State_Name
        FROM    Application_Leg AS app 
        INNER JOIN
                State AS state 
        ON      app.State_Id = state.ID
        WHERE   app.State_Id IN (5, 6, 8)
                AND app.Organisation_Id = 12 
                AND EXISTS
                (
                SELECT  Leg_Number 
                FROM    Application_Leg AS al
                INNER JOIN
                        Organisation as org
                ON      al.Organisation_Id = org.ID
                WHERE   al.ID = app.ID
                        AND al.LegNumber = app.LegNumber
                        AND org.Approval_Required = 1
                        AND Mode_Of_Transport = 1
                )
        ) AS pagedApplications
ORDER BY
        ReferenceNumber DESC


  1. SQL - Meerdere waarden door komma's gescheiden bij gebruik van GROUP BY

  2. Naam huidige uitvoeringsprocedure

  3. ALTER TABLE tabel AUTO_INCREMENT =$x

  4. Evaluatie van veelvouden 'IN' Expressies in 'WHERE'-clausules in mysql