Oracle set operation functions

Source: Internet
Author: User

The oracle set operation function set operator is used to merge the results of multiple select statements, including UNION, union all, INTERSECT, and MINUS. When using the set operator, make sure that the number of columns in Different queries matches the data type.
The set operator has the following considerations: * The set operator is not applicable to LOB, VARRAY, and nested columns. * The UNION, INTERSECT, and MINUS operators are not used for LONG columns. * If the selection list contains expressions or functions, the column alias must be defined for the expression or function. 1. UNION (UNION-free): When UNION is executed, duplicate rows in the result set are automatically removed and sorted in ascending order based on the results in the first column. 2. union all (with UNION set): duplicate rows are not removed and the result set is not sorted. 3. INTERSECT (intersection): obtains the intersection of the two result sets and sorts the results in ascending order in the first column. Select id, name, job from worker INTERSECT select empno, ename, job from emp; 4. MINUS (difference set): only shown in the first set, data that does not exist in the second collection. The results in the first column are sorted in ascending order. Select CUSTOMERNUMBER, DZMC, CREATETIME, LASTSENDTIME from userinfo hsu1, (select hsu. customerid, hsu. nameareainfoid from userinfo hsu where hsu. ispackage = 0 and hsu. SUBSTATUS = 0 and hsu. createtime <TO_DATE ('2017-03-05 ', 'yyyy-MM-DD') <p> MINUS select distinct hbr. customerid, hbr. nameid from revmagazineinfo hbr where hbr. SENDTIME <TO_DATE ('1970-03-05 ', 'yyyy-MM-DD') <p>) temp where hsu1.ISPACKAGE = 0 and hsu1.c Ustomerid = temp. customerid and hsu1.nameareainfoid = temp. nameareainfoid and hsu1.SUBSTATUS = 0 and hsu1.createtime <TO_DATE ('2017-03-05 ', 'yyyy-MM-DD') <p> SELECT * FROM (SELECT hsu. customerid, hsu. customernumber, hsu. MAGAZINEID, hsu. nameareainfoid FROM userinfo hsu WHERE hsu. ispackage = 0 AND (hsu. substatus = 0 OR hsu. substatus = 3) AND hsu. nameareainfoid IN (select distinct mi. nameid FROM magazineinfo Mi WHERE mi. pubtime <sysdate and mi. status = 1) and not exists (select 1 from revmagazineinfo hbr where hbr. customerid = hsu. customerid and hbr. nameid = hsu. nameareainfoid) 5. In addition, order by must be placed after the last select statement. When the column names are the same, they can be directly sorted by column names. If they are different, they can be sorted by position, you can also use aliases to make them the same. Select id, name x from new_emp union all select empno, ename x from emp order by x; // If the column names are not sorted by aliases, select id, name ename from new_emp union all select empno, ename from emp order by ename; // The column names are sorted by select id, name ename from new_emp union all select empno, ename from emp when aliases are not used at the same time; // after merging, the column name is displayed as the master table.

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.