UNION中ORDER By的使用

遇到的问题:
一个sql中,union了几个子查询。单独执行每个子查询都没问题,但union后执行,报
ORA-00904: "xxx": invalid identifier

关于union的使用:
sql: UNION Query:
http://www.techonthenet.com/sql/union.php
sql: UNION ALL Query:
http://www.techonthenet.com/sql/union_all.php
所union的各个子查询要有相同数量的列,且对应位置的列必须具有相同的数据类型;但列的名字可以不同。
the diffrence between UNION ALL and UNION is that UNION will attempt to eliminate duplicates.


关于order by的使用:
sql: ORDER BY Clause
http://www.techonthenet.com/sql/order_by.php
Example #3
You can also sort by relative position in the result set,where the first field in the result set is 1. The next field is 2,and so on.

sql代码

  1. SELECTsupplier_city

  2. FROMsuppliers

  3. WHEREsupplier_name='IBM'

  4. ORDERBY1DESC;


This would return all records sorted by the supplier_city field in descending order,since the supplier_city field is in position #1 in the result set.
union中order by的使用:
You have to use the Order By at the end of ALL the unions。
the ORDER BY is considered to apply to the whole UNION result(it's effectively got lower binding priority than the UNION).
The ORDER BY clause just needs to be the last statement,after you've done all your unioning. You can union several sets together,then put an ORDER BY clause after the last set.
所以,能在union的最后一个子查询中使用order by,而这个order by是针对整个unioning后的结果集的。So:
如果unoin的几个子查询列名不同,如

sql代码

复制代码

收藏代码

  1. selectsupplier_id,supplier_name

  2. fromsuppliers

  3. UNION

  4. selectcompany_id,company_name

  5. fromcompanies

  6. ORDERBY?;


这里的问号如果是company_name,则执行整个查询会报“company_name:invalid identifier”(当然,单独执行第二个含order by的子查询是没有问题的);这是因为unioning后结果集的列名是以第一个参加union的子查询的列名为准的;order by针对的是整个unioning后的结果集。对整个查询结果来说,无”company_name“这个字段
如果是supplier_name,则单独执行第二个含order by的子查询是会报“supplier_name:invalid identifier”的,而执行整个查询是没有问题的,因为order by针对的是unioning后的整个结果集,而这“整个结果集”是有supplier_name这列的(以第一个union子查询的列名作为unioning后整个结果集的列名)
为了避免这样事情的发生,可以:
1 使用列序号代替实际列名。如:
fromcompanies

  • ORDERBY2;


  • 2 为unoin的各个子查询使用相同的列名,如:

    sql代码

    1. selectsupplier_idasid,supplier_nameasname

    2. fromsuppliers

    3. UNION

    4. selectcompany_idasid,company_nameasname

    5. fromcompanies

    6. ORDERBYname;


    这样,不管是执行整个查询还是单独执行包含order by的最后一个union子查询,都不会有问题。
    Q&A:
    http://p2p.wrox.com/sql-language/9505-order-union.html
    Q:

    引用

    I have two tables,TableA and TableB defined as follows,

    TableA
    A1 int
    A2 int
    A3 int

    TableB
    B1 int
    B2 int
    B3 int

    If I try to run this query,sql Server says syntex Failed at the Order By clouse. Is such Order by not allowed in sql,Any other way to achieve this?

    sql代码

    1. (SelectA1,A2fromTableA)

    2. UnionAll

    3. (SelectB1,B2fromTableBOrderbyB3)


    Any help will be appreciated.


    A:

    引用

    First of all,you can not order by a column that is not included in your SELECT list(我注:这句话是错误的;可以order by一个不在select列表中的column). Secondly,when performing a UNION query the ORDER BY clause must be(我注:not “must be”!)a column index not a column name,becausea UNION query does not have column headings (although sql Server(我注:此处泛指DBMS)pretends that it has by picking the column names used in the first queryalthough this is not ANSI compliant]). Assuming you want to order the second column (A2 and B2) your query should look like this:
    Code:

    sql代码

    1. SELECTA1,A2

    2. FROMTableA

    3. UNIONALL

    4. SELECTB1,B2

    5. FROMTableB

    6. ORDERBY2



    Conceptually,ORDER BY works by producing the final query table with all the queries joined together (if it is a UNION query),then it orders the query results and does not care about what is in the database.

    相关文章

    数据库版本:11.2.0.4 RAC(1)问题现象从EM里面可以看到,在23号早上8:45~8:55时,数据库等待会话暴增...
    (一)问题背景最近在对一个大约200万行数据的表查看执行计划时,发现存在异常,理论上应该返回100多万...
    (一)删除备份--DELETE命令用于删除RMAN备份记录及相应的物理文件。当使用RMAN执行备份操作时,会在RM...
    (1)DRA介绍 数据恢复顾问(Data Recovery Advise)是一个诊断和修复数据库的工具,DRA能够修复数据文...
    RMAN(Recovery Manager)是Oracle恢复管理器的简称,是集数据库备份(backup)、修复(restore)和恢复...
    (1)备份对象 可以使用RMAN进行的备份对象如下: --整个数据库:备份所有的数据文件和控制文件; --数...