我正在创建一个包含少量表的视图,最后我想按降序排序数据。 当我在create view语句中使用ORDER BY子句时,我收到一条错误消息,指出ORDER BY子句不能包含在视图中。因此我使用SELECT TOP 100 PERCENT和ORDER BY子句。但是我的视图输出中有超过100行。 我该怎么做。帮助我。
创建不带order by子句的视图语句如下。
CREATE VIEW VW_PF_GOALEVAL_REPORT AS
select GEV.EVAL_ID, EV.EVAL_NAME, GEV.FORM_ID,
GEV.EMP_NUMBER,EMP.EMP_DISPLAY_NAME, EMPS.EMP_DISPLAY_NAME AS SUP_NAME, EMPR.EMP_DISPLAY_NAME AS REV_NAME,
GG.GGRP_ID, GG.GGRP_NAME, GG.GGRP_WEIGHTAGE,
GEV.GOAL_ID, G.GOAL_DESC, GEV.GOAL_VERSION_NO,
G.GOAL_WEIGHTAGE, G.GOAL_MANDATORY_FLG, G.TARGET_DATE,
EG.RETME_ID,
REE.RETMEITEM_GRADE AS EMP_RETMEITEM_ID, REE.RETMEITEM_DESCRIPTION AS EMP_RETMEITEM_DESC, GEV.GOALEVAL_EMP_RATING, GEV.EMP_COMMENT,
RES.RETMEITEM_GRADE AS SUP_RETMEITEM_ID, RES.RETMEITEM_DESCRIPTION AS SUP_RETMEITEM_DESC, GEV.GOALEVAL_SUP_RATING, GEV.SUP_COMMENT,
RER.RETMEITEM_GRADE AS REV_RETMEITEM_ID, RER.RETMEITEM_DESCRIPTION AS REV_RETMEITEM_DESC, GEV.SECOND_SUP_RATING, GEV.SECOND_SUP_COMMENT
from PF_GOALEVAL GEV
LEFT JOIN
PF_EVALUATION EV
ON GEV.EVAL_ID=EV.EVAL_ID
left join
PF_GOAL G
ON GEV.GOAL_ID=G.GOAL_ID AND GEV.EMP_NUMBER=g.EMP_NUMBER AND GEV.EVAL_ID=G.EVAL_ID and GEV.GOAL_VERSION_NO=G.GOAL_VERSION_NO
left join
PF_GOAL_GROUP GG
ON G.GOAL_TYPEID=GG.GGRP_ID
left join
PF_EVALGOAL_GROUP EG
ON GEV.EVAL_ID=EG.EVAL_ID AND GG.GGRP_ID=EG.GGRP_ID
left join
PF_RETINGMETHODITEMS REE
ON EG.RETME_ID=REE.RETME_ID AND GEV.GOALEVAL_EMP_RATING=REE.RETMEITEM_ID
left join
PF_RETINGMETHODITEMS RES
ON EG.RETME_ID=RES.RETME_ID AND GEV.GOALEVAL_SUP_RATING=RES.RETMEITEM_ID
left join
PF_RETINGMETHODITEMS RER
ON EG.RETME_ID=RER.RETME_ID AND GEV.SECOND_SUP_RATING=RER.RETMEITEM_ID
left join
PF_EVALEMPLOYEE EVEMPS
ON GEV.EMP_NUMBER=EVEMPS.EMP_NUMBER AND GEV.EVAL_ID=EVEMPS.EVAL_ID AND EVEMPS.APTYPE_ID=2
left join
EMPLOYEE EMPS
ON EVEMPS.PERAPPRAISER_NUMBER = EMPS.EMP_NUMBER
left join
PF_EVALEMPLOYEE EVEMPR
ON GEV.EMP_NUMBER=EVEMPR.EMP_NUMBER AND GEV.EVAL_ID=EVEMPR.EVAL_ID AND EVEMPR.APTYPE_ID=5
left join
EMPLOYEE EMPR
ON EVEMPR.PERAPPRAISER_NUMBER = EMPR.EMP_NUMBER
left join
EMPLOYEE EMP
ON GEV.EMP_NUMBER=EMP.EMP_NUMBER
left join
PF_EVALAPPRAISER EVA
ON gev.EVAL_ID = EVA.EVAL_ID
最后我想包括
order by gev.GOAL_VERSION_NO DESC
语句。 谢谢。
答案 0 :(得分:0)
不是说100行,而是说100%表示所有记录
更改第一个选择
CREATE VIEW VW_PF_GOALEVAL_REPORT AS
select TOP 100 PERCENT GEV.EVAL_ID, EV.EVAL_NAME, GEV.FORM_ID,
答案 1 :(得分:0)
您不应在视图中使用Order By。如果你使用前100%,那么你可以忽略错误,但你不会得到排序结果。
我可以建议你这样使用。
Select * from VW_PF_GOALEVAL_REPORT VW
order by VW.GOAL_VERSION_NO DESC
假设您想要在视图本身中包含order by语句,您可以使用Top 99%。在这里,您可以获得排序结果,但有可能错过结果集中的几行。请在视图外使用订单。