我正在阅读一些工作中的旧代码,并注意到有几个带有order by 1子句的视图。这有什么作用?
order by 1
例子:
Create view v_payment_summary AS SELECT A.PAYMENT_DATE, (SELECT SUM(paymentamount) FROM payment B WHERE PAYMENT_DATE = B.PAYMENT_DATE and SOME CONDITION) AS SUM_X, (SELECT SUM(paymentamount) FROM payment B WHERE PAYMENT_DATE = B.PAYMENT_DATE and SOME OTHER CONDITION) AS SUM_Y FROM payment A ORDER BY 1;
这个:
ORDER BY 1
…被称为“序数” - 该数字代表基于 SELECT 子句中定义的列数的列。在您提供的查询中,这意味着:
ORDER BY A.PAYMENT_DATE
这不是推荐的做法,因为: