ORDER BY子句在视图,内联函数,派生表,子查询中无效

时间:2015-08-13 07:32:02

标签: sql-server

我收到以下错误:

  

ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效,除非还指定了TOP,OFFSET或FOR XML。

当我尝试运行此查询时:

SELECT
MK_NAMA_KELAB AS "Nama Kelab",
MK_NO_AHLI AS "No Ahli",
convert(varchar(10),MK_TKH_KEAHLIAN_MULA,103) AS "Tarikh Mula",
convert(varchar(10),MK_TKH_KEAHLIAN_HINGGA,103) AS "Tarikh Hingga",
(MK_JUMLAH_YURAN) AS "Jumlah Yuran",
CASE  STATUS 
    WHEN 'Draf' THEN concat( '<a href =''','index.php?page=page_wrapper&','','menuID=26124&','pe_id=',PE_ID,'&','role=STAFF','''>',
                                          STATUS,'</a>')
    WHEN 'Sedang Dalam Pantauan' THEN concat('<a href =''','index.php?page=page_wrapper&','','menuID=26167','&','status=',STATUS,'&','pe_id=',PE_ID,'&','role=STAFF','''>',
                                          STATUS,'</a>' )                
    ELSE  concat('<a href =''','index.php?page=page_wrapper&','','menuID=26167&','pe_id=',PE_ID,'&','role=STAFF','''>',STATUS,'</a>')
END AS "Status"
FROM (
SELECT MK_PE_ID AS PE_ID,
MK_NAMA_KELAB,MK_NO_AHLI,
MK_TKH_KEAHLIAN_MULA,
MK_TKH_KEAHLIAN_HINGGA,
MK_JUMLAH_YURAN,
(SELECT LHP_NAMA FROM LKP_HR_PERAKUAN WHERE LHP_ID=
(isnull(
(SELECT PP_STATUS FROM E_PENGESAHAN_PERMOHONAN WHERE PP_ID =
 (SELECT MAX(PP_ID) FROM E_PENGESAHAN_PERMOHONAN WHERE PP_PE_ID=MK_PE_ID)),
(SELECT LP_STATUS FROM E_LOG_PERMOHONAN WHERE LP_ID =
 (SELECT MAX(LP_ID) FROM E_LOG_PERMOHONAN WHERE LP_PE_ID=MK_PE_ID))))) STATUS
FROM E_MAKLUMAT_KEAHLIAN
JOIN E_PERMOHONAN_ELAUN ON PE_ID=MK_PE_ID
WHERE PE_STF_ID = (
    SELECT STF_ID 
    FROM HR_STAF_PERIBADI SP 
    JOIN PRUSER PP ON PP.USERNAME = STF_USERNAME
    WHERE PP.USERID = '{SESSION|userID}')

ORDER BY MK_TARIKH_SIMPAN DESC)

2 个答案:

答案 0 :(得分:2)

错误可能与此子查询有关:

FROM (
SELECT MK_PE_ID AS PE_ID,
...
ORDER BY MK_TARIKH_SIMPAN DESC)

子查询返回一个无序集,因此对它进行排序没有意义。当您尝试无目的地订购时,SQL Server将引发错误。

order by移至外部查询:

FROM    (
        SELECT MK_PE_ID AS PE_ID,
        ...
        ) as SubQueryAlias
ORDER BY MK_TARIKH_SIMPAN DESC

子查询也需要别名。

答案 1 :(得分:0)

尝试以下查询。请参阅我不知道您的表格结构。但问题是您在“发件人”内添加订单。

    SELECT MK_NAMA_KELAB AS "Nama Kelab",MK_NO_AHLI AS "No Ahli",convert(varchar(10),MK_TKH_KEAHLIAN_MULA,103) AS "Tarikh Mula",convert(varchar(10),MK_TKH_KEAHLIAN_HINGGA,103) AS "Tarikh Hingga",(MK_JUMLAH_YURAN) AS "Jumlah Yuran",
CASE  STATUS 
    WHEN 'Draf' THEN concat( '<a href =''','index.php?page=page_wrapper&','','menuID=26124&','pe_id=',PE_ID,'&','role=STAFF','''>',
                                          STATUS,'</a>')
    WHEN 'Sedang Dalam Pantauan' THEN concat('<a href =''','index.php?page=page_wrapper&','','menuID=26167','&','status=',STATUS,'&','pe_id=',PE_ID,'&','role=STAFF','''>',
                                          STATUS,'</a>' )                
    ELSE  concat('<a href =''','index.php?page=page_wrapper&','','menuID=26167&','pe_id=',PE_ID,'&','role=STAFF','''>',STATUS,'</a>')
END AS "Status",
MK_TARIKH_SIMPAN
FROM (
            SELECT MK_PE_ID AS PE_ID,
            MK_NAMA_KELAB,
            MK_NO_AHLI,
            MK_TKH_KEAHLIAN_MULA,
            MK_TKH_KEAHLIAN_HINGGA,
            MK_JUMLAH_YURAN,
            MK_TARIKH_SIMPAN,
            (
                SELECT LHP_NAMA FROM LKP_HR_PERAKUAN 
                WHERE LHP_ID=
                            (isnull(
                                        (SELECT PP_STATUS FROM E_PENGESAHAN_PERMOHONAN WHERE PP_ID =
                                                (SELECT MAX(PP_ID) FROM E_PENGESAHAN_PERMOHONAN WHERE PP_PE_ID=MK_PE_ID)
                                        ),
                                        (SELECT LP_STATUS FROM E_LOG_PERMOHONAN WHERE LP_ID =
                                                        (SELECT MAX(LP_ID) FROM E_LOG_PERMOHONAN WHERE LP_PE_ID=MK_PE_ID)
                                        )

                                    ))
            ) STATUS
            FROM E_MAKLUMAT_KEAHLIAN
            JOIN E_PERMOHONAN_ELAUN ON PE_ID=MK_PE_ID
            WHERE PE_STF_ID = (
                SELECT STF_ID 
                FROM HR_STAF_PERIBADI SP 
                JOIN PRUSER PP ON PP.USERNAME = STF_USERNAME
                WHERE PP.USERID = '{SESSION|userID}'
    )

)
ORDER BY MK_TARIKH_SIMPAN DESC