How to Merge Two Date Columns in Oracle
Oracle is a computer system that allows you to build reports from raw data that is housed in a database. Oracle supports the PL/SQL and SQL languages. One of the attributes that you can use when building out reports is the MERGE statement. This statement allows you to select rows from one or more sources and update or insert the data into a table. With the MERGE statement, you cannot update the same row in the table multiple times. Whether the table is going to be updated or inserted is based on the commands in the ON clause of the MERGE statement.
Instructions
-
-
1
Open Oracle. Type your username in the text field for username. Type your password in the text field for password. Type your database in the text field for database or use the list of values (down arrow) to select a database.
-
2
Create two practice queries. In the first query, select three columns--for example, CUST_ID, DATE and VALUE from client 1. Save query one as "CHANGE_DATE_A." Create a second, but use client 2 with columns CUST_ID, DATE and VALUE. Save your second query as "NEW_DATE_B." Query two will be used to merge your columns in both files. Query one and two can contain any of the attributes that you want to use. SELECT, FROM and ORDER BY are a few attributes that you will want to use to generate date columns. Press the execute icon.
-
-
3
Type "MERGE INTO NEW_DATE_B ND" in query two, under your original query. This is instructing Oracle that you will be merging data into your NEW_DATE_B file. It is also assigning your NEW_DATE B file the nickname of ND. This will save time and space in your command procedure.
-
4
Type "USING CHANGE_DATE_A CD" under MERGE INTO, directly under the "G" in MERGE. This is instructing Oracle that you will be merging your CHANGE_DATE_A file into your NEW_DATE_B file. It is also assigning your CHANGE_DATE_A file a nickname of CD.
-
5
Type "ON (CD.CUST_ID=ND.CUST_ID and CD.DATE=ND.DATE)" directly below "USING." This is instructing Oracle to merge the data under the CUST_ID column on both files into one column. It is also instructing Oracle to merge the date under the DATE column on both files into one column.
-
6
Type "WHEN MATCHED THEN UPDATE SET ND.VALUE=CD.VALUE" directly below "ON." This is instructing Oracle to match the data under the VALUE column in both files.
-
7
Type "WHEN NOT MATCHED THEN INSERT VALUES (CUST_ID, DATE, VALUE);". This is instructing Oracle to fill in the data previously provided in both files without merging the two files together if any of the data is not matched. In the results, any unmatched data will be listed. Press the execute icon. The query results will return three columns, CUST_ID, DATE and VALUE, with the data from both the CHANGE_DATE_A file and the NEW_DATE_B file. The difference is that the data that is generated under each column now contains the data from client 1 and 2.
-
1