1

以下のように、クエリの最も外側の選択で DISITINCT を使用するときに、row_number() 関数を使用して順序付けの結果が降順になるときに見られる異常な動作を理解しようとしています。

 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 = 5 OR app.State_Id = 6 OR app.State_Id = 8)  AND app.Organisation_Id=12 
    AND Leg_Number IN
     (SELECT  DISTINCT 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)

最も外側の DISTINCT が取り出された場合は降順で問題ありませんが、含まれている場合は結果が降順で表示されません。

4

2 に答える 2

9

ORDER BYinROW_NUMBER句は、結果セットの順序を保証しません。

ROW_NUMBER通常、クエリプランで並べ替えを使用すると、値が事前に並べ替えられます。

これは副作用であり、信頼すべきではありません。

DISTINCTHash Match (Aggregate)ソートを破る用途。

ORDER BYクエリの最後に句を追加します。

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

10また、個別の結果が返されるDISTINCTのではなく、最初の結果が返されることに注意してください10

前者が必要な場合は、これを使用します。

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
于 2009-09-24T14:07:06.253 に答える
0

外側の選択に order by を追加しようとしましたか?

于 2009-09-24T14:06:53.493 に答える