Download presentation
Presentation is loading. Please wait.
1
Set Operations Part VI
2
Set Operations Allow you to combine the results of two or more queries. UNION ALL UNION MINUS INTERSECT
3
Requirements Will This Work?
Result sets of all queries being combined must have the same number of columns The data type of each column in the 2nd and subsequent result sets must match the data type of its corresponding column in the 1st result set SELECT spriden_id ,spriden_last_name … UNION SELECT sgbstdn_pidm ,sgbstdn_term_code_eff VARCHAR2(9) VARCHAR2(60) … NUMBER(8) VARCHAR2(6) Will This Work?
4
UNION ALL Combines the results of multiple SELECT statements into one result set SELECT 'SHREVNT’ AS Table_Code, shrevnt_pidm AS PIDM, spriden_id AS CSU_ID, spriden_last_name || ', ' || spriden_first_name AS Name, shrevnt_levl_code AS Levl, shrevnt_desc AS Description, shrevnt_activity_date AS Activity_Date, shrevnt_orig_code AS Orig_Code, FROM shrevnt, spriden WHERE spriden_pidm = shrevnt_pidm AND spriden_change_ind IS NULL AND shrevnt_effective_date IS NULL UNION ALL SELECT 'SHRTMCM’ AS Table_Code, shrtmcm_pidm AS PIDM, spriden_id AS CSU_ID, shrtmcm_levl_code AS Levl, shrtmcm_comment AS Description, shrtmcm_activity_date AS Activity_Date, shrtmcm_orig_code AS Orig_Code, FROM shrtmcm, spriden WHERE spriden_pidm = shrtmcm_pidm AND spriden_change_ind IS NULL AND shrtmcm_effective_date IS NULL ORDER BY Table_Code, Name, CSU_ID Selects records that have null values for the effective date in tables related to academic history.
5
UNION Combines the result set of multiple SELECT statements into one result set, then eliminates duplicate rows from the result set
6
MINUS Takes the result set of the first SELECT statement and removes any rows also returned by subsequent SELECT statements Duplicate rows are eliminated -- get all person records SELECT i.spriden_pidm FROM spriden i WHERE i.spriden_entity_ind = 'P’ MINUS -- remove if application exists SELECT saradap_pidm FROM saradap -- remove if transcript comment exists SELECT shrtmcm_pidm FROM shrtmcm
7
INTERSECT Returns only those rows returned by each of the SELECT statements Duplicate rows are eliminated SELECT spriden_id csu_id ,spriden_last_name || ', ' || spriden_first_name name FROM sgrvetn JOIN spriden ON spriden_pidm = sgrvetn_pidm AND spriden_change_ind is null INTERSECT FROM sgrsprt ON spriden_pidm = sgrsprt_pidm ORDER BY name
8
Intersect Why not this instead? SELECT distinct spriden_id csu_id
,spriden_last_name || ', ' || spriden_first_name name FROM sgrvetn JOIN spriden ON spriden_pidm = sgrvetn_pidm AND spriden_change_ind is null JOIN sgrsprt ON sgrsprt_pidm = sgrvetn_pidm ORDER BY name
9
Rules / Restrictions Column names used in the result set are derived from the first SELECT statement ORDER BY can appear only once and it must be after the final SELECT statement Column names used in the ORDER BY must be those defined in the first SELECT statement GROUP BY and HAVING may appear in any of the SELECT statements
10
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.