Oracle order by and union

Source: Internet
Author: User


Use of oracle order by and union statements when union operations are used, the sorting statement must be placed at the end of the statement. For example: www.2cto.com can only use order by in the last subquery of union, this order by is for the result set after the entire unioning. So: if the names of several subqueries in unoin are different, for example
SQL code select supplier_id, supplier_name from suppliers UNION select company_id, company_name from companies ORDER ?; If the question mark is company_name, "company_name: invalididentifier" is returned when the entire query is executed. (Of course, it is no problem to execute the second subquery containing orderby separately ); this is because the column name of the result set after unioning is the first column name that participates in the union subquery. orderby is for the result set after unioning. For the entire query result, if the field www.2cto.com without the "company_name" is supplier_name, executing the second subquery with order by separately will report "supplier_name: invalididentifier, there is no problem in executing the entire query, because orderby is for the entire result set after unioning, the "entire result set" contains the supplier_name column (the column name of the first union subquery is used as the name of the whole result set after unioning). To avoid such a problem, you can: 1. Replace the actual column name with the column number. For example, SQL code select supplier_id, supplier_name from suppliers UNION select company_id, company_name from companies ORDER BY 2; www.2cto.com 2, or select * from (SELECT SALING_DATE, JOB, name from TABLE1 union select ARRIVE_DATE SALING_DATE, JOB, name from TALBE2) order by SALING_DATE;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.