2016-06-09 2 views
1

굿디, 먼저 내 질문을 확인하십시오. 내가 Order by 내 쿼리 오류를 추가있을 때주문을 추가 할 때 오류가 발생했습니다.

SELECT * 
FROM 
( 
    SELECT DISTINCT row, a.tanggal, b.OutletCode, c.Nilai, a.Nip, b.Fullname, 
     a.KodePenilaian, f.Description AS posisilama, d.ShortDesc AS posisibaru 
    FROM penilaian_header a 
    LEFT JOIN Employee b 
     ON a.Nip = b.Nip 
    LEFT JOIN Position f 
     ON b.PositionCode = f.PositionCode 
    LEFT JOIN Position d 
     ON a.PositionCode = d.PositionCode 
    LEFT JOIN arealeader g 
     ON g.OutletCode = b.OutletCode 
    LEFT JOIN 
    (
     SELECT ROW_NUMBER() OVER (PARTITION BY KodePenilaianH 
            ORDER BY idPenilaiand DESC) AS Row, 
      Nilai, KodePenilaianH 
     FROM penilaian_Detail 
    ) c 
     ON a.KodePenilaian = c.KodePenilaianH 
    WHERE a.Outlet LIKE '%%' AND Periode LIKE '%%' 
    ORDER BY b.OutletCode ASC 
) nilai PIVOT (SUM(nilai) FOR ROW IN ([1],[2],[3],[4],[5])) piv; 

내 문제입니다. 다음은 오류입니다.

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified. 

Order By 내 쿼리가 정상적으로 작동합니다.

+1

오류 메시지는 매우 명확합니다. 하위 쿼리에서'ORDER BY '를 사용하고 있습니다. 하위 쿼리에'TOP '을 추가 할 수 있습니까? –

답변

2

저는 ORDER BY b.OutletCode ASC이 파티션 내부의 ORDER BY이 아니라 오류의 원인이라고 생각합니다.이 것이 필요하고 허용되어야합니다.

모든 레코드를 반환하려면 TOP과 같이 큰 번호를 사용할 수 있습니다.

SELECT * 
FROM 
( 
    SELECT DISTINCT TOP 2147483647 row, a.tanggal, b.OutletCode, c.Nilai, a.Nip, 
     b.Fullname, a.KodePenilaian, f.Description AS posisilama, d.ShortDesc AS posisibaru 
    FROM penilaian_header a 
    LEFT JOIN Employee b 
     ON a.Nip = b.Nip 
    ... 
    ORDER BY b.OutletCode ASC 
) nilai PIVOT (SUM(nilai) FOR ROW IN ([1],[2],[3],[4],[5])) piv; 
+0

나에게 위대한 교훈이다. 감사. – YVS1102

+0

좀 더 철저한 토론을 원하면 [this so answer] (http://stackoverflow.com/questions/985921/sql-error-with-order-by-in-subquery)를보십시오. –

관련 문제