添加订单时出错

时间:2016-06-09 03:49:23

标签: sql sql-server-2008

Goodday,请先检查我的查询。

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;

我的问题是当我添加Order by我的查询错误时。这是错误:

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 个答案:

答案 0 :(得分: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;