Oracle merge query




===
  union union
  , intersect, union all union+intersection, minus difference First find out the results produced by the first SQL statement, and then see if these results are in the results of the second SQL statement. If there is, the record is removed and will not appear in the final result. If the result produced by the second SQL statement does not exist in the result produced by the first SQL statement, the data is discarded, and the syntax is as follows:
  [SQL Segment 1]
  MINUS
  [SQL Segment 2]
2. union all keyword
In set theory, the union of two sets (set A and set B) is a set containing all the elements in sets A and B. In other words, if an element belongs to any of the input sets, then it also belongs to the result set. as the picture shows.



  For set operations, the following points need to be noted:

The result sets generated by the two queries participating in the set operation must contain the same number of columns, and the corresponding columns must have compatible data types.
The column names in the result of a set operation are determined by the first query, so if you want to assign an alias to the result column, you should assign the corresponding alias in the first query.
When set operations compare rows, two NULL values ​​are considered equal.
The UNION set operation is divided into two cases: UNION ALL and UNION DISTINCT. The difference between them is that UNION ALL will retain duplicate rows, while UNION DISTINCT will delete duplicate rows. The following is an example to illustrate.

back to the top
UNION ALL

UNION ALL merges two sets, keeping duplicate rows. The following points need to be noted about UNION ALL:

UNION ALL does not actually compare rows, nor does it delete duplicate rows, it just does a merge operation.
Because UNION ALL does not remove duplicate rows, its result is a multiset, not a true set, since the same row may appear multiple times in the result.

3.INTERSECT (intersection) set operation
In set theory, the intersection of two sets (denoted as sets A and B) is a set composed of all elements that belong to both A and B.

In T-SQL, the INTERSECT set operation takes the intersection of the result sets of two input queries and returns only the rows that appear in both query result sets.

INTERSECT DISTINCT Set Operation

The INTERSECT set operation logically first removes duplicate rows in the two input multisets (turning the multiset into a set), and then returns only the rows that appear in both sets. In other words, if a row occurs at least once in both input multisets, the result returned by the intersection will include that row.

SELECT c FROM a

INTERSECT

SELECT d FROM b

INTERSECT ALL Aggregate Operations

ANSI SQL supports INTERSECT aggregate operations with the ALL option, but SQL Server 2008 does not implement this operation yet.

Recall the meaning of the ALL keyword in the UNION ALL set operation: to request that all duplicate rows be returned. Similarly, the ALL keyword in the INTERSECT ALL set operation also means that duplicate rows are not removed. But INTERSECT ALL is different from UNION ALL: the former does not return all duplicate rows, but only returns all duplicate rows of the multiset with a smaller number of duplicate rows. In other words, the INTERSECT ALL operation cares not only whether a row exists in both multisets, but also the number of times it appears in each multiset. It's as if this set operation would find every match for every row.

SELECT 

  ROW_NUMBER() OVER(PARTITION BY country,region,city ORDER BY(SELECT 0)) AS rownum,

  country,region,city

FROM dbo.Employees

INTERSECT

SELECT

  ROW_NUMBER() OVER(PARTITION BY country,region,city ORDER BY (SELECT 0)),

  country,region,city

FROM dbo.Customers;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326927788&siteId=291194637