Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

SQL Server 2005 - Row_Number()

ORDER BY di ROW_NUMBER klausa tidak menjamin urutan hasil.

ROW_NUMBER biasanya menggunakan pengurutan dalam rencana kueri yang menghasilkan fakta bahwa nilai-nilai yang keluar telah disortir.

Ini adalah efek samping dan tidak boleh diandalkan.

DISTINCT menggunakan Hash Match (Aggregate) yang merusak penyortiran.

Tambahkan ORDER BY klausa ke akhir kueri:

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

Perhatikan juga bahwa itu tidak akan mengembalikan 10 hasil yang berbeda, itu akan mengembalikan DISTINCT dari 10 pertama hasil.

Jika Anda menginginkan yang pertama, gunakan ini:

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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Dapatkan skema kolom tabel SQL dengan Tipe Data dan Panjang Maks dalam nilai angka

  2. Kontrol aliran di T-SQL SP menggunakan IF..ELSE IF - apakah ada cara lain?

  3. openrowset untuk excel:bisakah kita melewati beberapa baris?

  4. SQL Server:Kembalikan tanpa MULAI TRANSAKSI

  5. Cara menyalin data tabel besar ke tabel lain di SQL Server