我需要从表中选择一些行,但这些行必须由ROWNUMBER asc
排序,如果ROWNUMBER
为空,则必须按created desc
排序;
这是我的sql查询,没有按条件排序;
SELECT FIN_CGU.*, RES.ROWNUMBER
FROM COMM$GROUP_USER FIN_CGU
FULL JOIN (SELECT AGGR_RES.R_GR AS TOT_GR,
ROW_NUMBER() OVER(ORDER BY AGGR_RES.M_DATE DESC) AS ROWNUMBER
FROM (SELECT MSG.GROUP_ID AS R_GR, MAX(MSG.CREATED) AS M_DATE
FROM COMM$MESSAGE MSG
JOIN COMM$GROUP_USER GR_USR
ON GR_USR.GROUP_ID = MSG.GROUP_ID
JOIN COMM$GROUP GR
ON GR.ID = MSG.GROUP_ID
WHERE GR.STATE <> 'DELETED'
AND GR_USR.STATE <> 'DELETED'
AND MSG.STATE <> 'DELETED'
AND GR_USR.USER_ID = 9172771513163989084
GROUP BY MSG.GROUP_ID) AGGR_RES
ORDER BY AGGR_RES.M_DATE DESC) RES
ON FIN_CGU.GROUP_ID = RES.TOT_GR
JOIN COMM$GROUP CG
ON FIN_CGU.GROUP_ID = CG.ID
WHERE (ROWNUMBER IS NULL OR ROWNUMBER BETWEEN 1 AND 3)
AND FIN_CGU.USER_ID = 9172771513163989084
AND RES.ROWNUM <= 3
ORDER BY
Oracle 10g;
答案 0 :(得分:1)
一旦列列中有该列,您的内部查询应该提供排序;所以您可以通过案例或 nvl 执行以下操作:
按顺序排列,当ROWNUMBER不为空时,则ROWNUMBER为其他人创建结束
您无法在静态查询中动态触发同一列列表上的asc | desc。你可以在动态SQL中做到这一点。您可以更改订购列虽然(显示)