[英]Oracle - where the orderly column ID e.g. ORDER BY 1 is allwed?
What are the rules where the orderly column ID 1, 2, ... is allowed?允许有序列 ID 1、2、... 的规则是什么? From which part of document can I tell?
我可以从文件的哪一部分看出?
SELECT *
FROM (
SELECT Months * Salary, COUNT(*)
FROM Employee
GROUP BY (Months * Salary)
ORDER BY 1 DESC <---- This is OK
)
WHERE ROWNUM = 1;
----------
108064 7
SELECT *
FROM (
SELECT Months * Salary, COUNT(*)
FROM Employee
GROUP BY 1 <--- ORA-00979: not a GROUP BY expression
ORDER BY 1 DESC
)
WHERE ROWNUM = 1;
----------
SELECT Months * Salary, COUNT(*)
*
ERROR at line 3:
ORA-00979: not a GROUP BY expression
For this case, which document can tell it is not allowed and why?对于这种情况,哪个文件可以告诉它是不允许的,为什么?
GROUP BY
clause the 1
is a number literal value.GROUP BY
子句中, 1
是一个数字文字值。ORDER BY
clause the 1
refers to the the first term of the SELECT
clause.ORDER BY
子句中, 1
指的是SELECT
子句的第一项。 If you do:如果你这样做:
SELECT *
FROM (
SELECT COUNT(*)
FROM Employee
GROUP BY 1 -- A number literal
ORDER BY 1 DESC
)
WHERE ROWNUM = 1;
It is the same as:它与以下内容相同:
SELECT *
FROM (
SELECT COUNT(*)
FROM Employee
GROUP BY NULL -- A NULL literal
ORDER BY 1 DESC
)
WHERE ROWNUM = 1;
or或者
SELECT *
FROM (
SELECT COUNT(*)
FROM Employee
GROUP BY 'ABC' -- A string literal
ORDER BY 1 DESC
)
WHERE ROWNUM = 1;
However,然而,
SELECT *
FROM (
SELECT Months * Salary, COUNT(*)
FROM Employee
GROUP BY 1
ORDER BY 1 DESC
)
WHERE ROWNUM = 1;
Is not valid as 1
is a literal number value that you are grouping by whereas Months
and Salary
are column names that are in a GROUP BY
query but are not aggregated.无效,因为
1
是您分组依据的文字数字值,而Months
和Salary
是GROUP BY
查询中的列名,但未聚合。
In the images only in order-by you can find the "position" option.在仅按顺序排列的图像中,您可以找到“位置”选项。 Also, from "Java DB Technical Documentation":
此外,来自“Java DB 技术文档”:
GROUP BY
{
column-Name [ , column-Name ]*
|
ROLLUP ( column-Name [ , column-Name ]* )
}
column-Name must be a column from the current scope of the query;
there can be no columns from a query block outside the current scope.
For example, if a GROUP BY clause is in a subquery,
it cannot refer to columns in the outer query.
ORDER BY { column-Name | ColumnPosition | Expression }
[ ASC | DESC ]
[ NULLS FIRST | NULLS LAST ]
[ , column-Name | ColumnPosition | Expression
[ ASC | DESC ]
[ NULLS FIRST | NULLS LAST ]
] *
ColumnPosition
An integer that identifies the number of the column in the SelectItems
in the underlying query of the SELECT statement.
ColumnPosition must be greater than 0 and not greater
than the number of columns in the result table. In other words,
if you want to order by a column, that column must be specified
in the SELECT list.
Positional integer column ID is available in ORDER BY but not in GROUP BY.位置 integer 列 ID 在 ORDER BY 中可用,但在 GROUP BY 中不可用。
position
position
Specify position to order rows based on their value for the expression in this position of the select list.
指定 position 以根据 select 列表的此 position 中的表达式的值对行进行排序。 The position value must be an integer .
position 值必须是 integer 。
You can specify multiple expressions in the order_by_clause.
您可以在 order_by_clause 中指定多个表达式。 Oracle Database first sorts rows based on their values for the first expression.
Oracle 数据库首先根据第一个表达式的值对行进行排序。 Rows with the same value for the first expression are then sorted based on their values for the second expression, and so on.
然后,对于第一个表达式具有相同值的行将根据它们在第二个表达式中的值进行排序,依此类推。 The database sorts nulls following all others in ascending order and preceding all others in descending order.
数据库按升序对所有其他空值进行排序,并按降序对所有其他空值进行排序。 Refer to "Sorting Query Results" for a discussion of ordering query results.
有关排序查询结果的讨论,请参阅“排序查询结果”。
To select the same information as the previous SELECT and use the positional ORDER BY notation, issue the following statement, which orders by ascending department_id, then descending salary, and finally alphabetically by last_name:
对 select 与之前的 SELECT 相同的信息并使用位置 ORDER BY 表示法,发出以下语句,该语句按部门 ID 升序,然后薪金降序,最后按姓氏字母顺序排列:
SELECT last_name, department_id, salary
FROM employees
ORDER BY 2 ASC, 3 DESC, 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.