简体   繁体   English

Oracle - 有序列 ID 例如 ORDER BY 1 被允许在哪里?

[英]Oracle - where the orderly column ID e.g. ORDER BY 1 is allwed?

Question问题

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?对于这种情况,哪个文件可以告诉它是不允许的,为什么?

在此处输入图像描述

在此处输入图像描述

  • In the GROUP BY clause the 1 is a number literal value.GROUP BY子句中, 1是一个数字文字值。
  • In the 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是您分组依据的文字数字值,而MonthsSalaryGROUP 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 中不可用。

Positional ORDER BY notation位置 ORDER 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.

相关问题 SQL的扩展占位符,例如WHERE id IN(??) - Extended placeholders for SQL, e.g. WHERE id IN (??) 使用Oracle SQL插入数字ID递增的行(例如rownum)? - Insert row with increasing numerical ID (e.g. rownum) using Oracle SQL? 初始化SortOrder列(例如0,1,2,3)的最佳方法是什么,其中有多个组基于另一个字段? - What is the best way to initialize a SortOrder column (e.g. 0, 1, 2, 3) where there are multiple groups based on another field? Oracle SQL:例如在4个小时内计数和求和? - Oracle SQL: Count and sum in e.g. 4 hours? SQL:将例如1,2,4,5,7修正为1,2,3,4,5(队列顺序) - SQL: Correct e.g. 1,2,4,5,7 to 1,2,3,4,5 (queue order) 如何按自定义规则订购,例如如何订购4,2,1,3 - How to order by custom rule, e.g. how to order like 4,2,1,3 如何在Oracle中为大于32k(例如60,000个字符)的CLOB生成JSON? - How to generate JSON in Oracle for a CLOB that is > 32k (e.g. 60,000 characters)? 使用 ORACLE SQL 修复字符串中损坏的字符(例如变音符号)并将其转换为正确的 UTF-8 - Fix corrupted characters (e.g. umlauts) in a string using ORACLE SQL and convert it to proper UTF-8 SQL与UNION一起使用ORDER BY不能正确地对数字排序(例如8之前的10) - SQL Using ORDER BY with UNION doesn't sort numbers correctly (e.g. 10 before 8) 选择字母开头或后面没有字母的字母,例如FIND Ag或* Ag,而不是SAG或MAGNET - Select letters where they are not preceeded or followed by letters e.g. FIND Ag or *Ag but not SAG or MAGNET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM