Merge with UNION
In the following example, two tables – customer and customer_analysis – are merged with the default UNION operator. The columns in the first table determine the column names, types, and aliases of the result table, in this case, the alias names cid and inc. The default output style SELECT is used, therefore, the results are returned in a data set.
Call:
call td_analyze('merge','
database=
val_source,
val_source;
tablename=
customer,
customer_analysis;
columns=
{cust_id/cid, income/inc},
{cust_id, income};
where=
{cust_id < 1362490},
{cust_id < 1362490};
');
SQL:
SELECT
"cust_id" AS "cid"
,"income" AS "inc"
FROM "val_source"."customer"
WHERE cust_id < 1362490
UNION
SELECT
"cust_id"
,"income" AS "income"
FROM "val_source"."customer_analysis"
WHERE cust_id < 1362490
Results:
| cid |
inc |
| 1362480 |
50890 |
| 1362481 |
20855 |
| 1362484 |
10053 |
| 1362485 |
22690 |
| 1362486 |
10701 |
| 1362487 |
6605 |
| 1362488 |
7083 |
| 1362489 |
55888 |
Merged with UNION, Persisted
This example has the same analysis performed as the previous example, but the results are persisted to a table using the parameters outputstyle, outputdatabase, outputtablename, and index. Note that the index is cid, the alias for cust_id in the columns parameter from the previous example. When a table is being produced (for instance, if outputstyle is table or volatile), any column or alias that appears in index must also appear in the first table listed in the columns parameter.
Call:
call td_analyze('merge','
database=
val_source,
val_source;
tablename=
customer,
customer_analysis;
columns=
{cust_id/cid, income/inc},
{cust_id, income};
where=
{cust_id < 1362490},
{cust_id < 1362490};
outputstyle=table;
outputdatabase=val_results;
outputtablename=merge_example2;
index=cid;
');
SELECT:
select * from val_results.merge_example2 order by 1;
SQL:
DROP TABLE "val_results"."merge_example2";
CREATE SET TABLE "val_results"."merge_example2", NO FALLBACK, NO BEFORE JOURNAL, NO AFTER JOURNAL AS (
SELECT
"cust_id" AS "cid"
,"income" AS "inc"
FROM "val_source"."customer"
WHERE cust_id < 1362490
UNION
SELECT
"cust_id"
,"income" AS "income"
FROM "val_source"."customer_analysis"
WHERE cust_id < 1362490
) WITH DATA PRIMARY INDEX ("cid");
Results:
| cid |
inc |
| 1362480 |
50890 |
| 1362481 |
20855 |
| 1362484 |
10053 |
| 1362485 |
22690 |
| 1362486 |
10701 |
| 1362487 |
6605 |
| 1362488 |
7083 |
| 1362489 |
55888 |
Merge with UNION, Volatile
In the following example, the same analysis is performed as the previous two examples, but the results are persisted to a volatile table using the parameters outputstyle=volatile, outputtablename=merge_example3, and noindex. Note that the outputdatabase parameter is not used in this example because volatile tables are automatically created in the user database.
Call:
call td_analyze('merge','
database=
val_source,
val_source;
tablename=
customer,
customer_analysis;
columns=
{cust_id/cid, income/inc},
{cust_id, income};
where=
{cust_id < 1362490},
{cust_id < 1362490};
outputstyle=volatile;
outputtablename=merge_example3;
noindex=true;
');
SELECT:
select * from merge_example3 order by 1;
SQL:
DROP TABLE "val_user"."merge_example3";
CREATE MULTISET VOLATILE TABLE "val_user"."merge_example3", NO FALLBACK, NO BEFORE JOURNAL, NO AFTER JOURNAL AS (
SELECT
"cust_id" AS "cid"
,"income" AS "inc"
FROM "val_source"."customer"
WHERE cust_id < 1362490
UNION
SELECT
"cust_id"
,"income" AS "income"
FROM "val_source"."customer_analysis"
WHERE cust_id < 1362490
) WITH DATA NO PRIMARY INDEX ON COMMIT PRESERVE ROWS;
Results:
| cid |
inc |
| 1362480 |
50890 |
| 1362481 |
20855 |
| 1362484 |
10053 |
| 1362485 |
22690 |
| 1362486 |
10701 |
| 1362487 |
6605 |
| 1362488 |
7083 |
| 1362489 |
55888 |
Merge with UNION, View
In the following example, the same analysis is performed as in all previous examples, except a database view is created by setting outputstyle=view. Note that neither the index or noindex parameters are used because they are incompatible with a view. Also note that the outputtablename parameter specifies a view rather than a table (for example, merge_example4 is a database view).
Call:
call td_analyze('merge','
database=
val_source,
val_source;
tablename=
customer,
customer_analysis;
columns=
{cust_id/cid, income/inc},
{cust_id, income};
where=
{cust_id < 1362490},
{cust_id < 1362490};
outputstyle=view;
outputdatabase=val_results;
outputtablename=merge_example4;
');
SELECT:
select * from val_results.merge_example4 order by 1;
SQL:
DROP VIEW "val_results"."merge_example4";
CREATE VIEW "val_results"."merge_example4" AS (
SELECT
"cust_id" AS "cid"
,"income" AS "inc"
FROM "val_source"."customer"
WHERE cust_id < 1362490
UNION
SELECT
"cust_id"
,"income" AS "income"
FROM "val_source"."customer_analysis"
WHERE cust_id < 1362490
);
Results:
| cid |
inc |
| 1362480 |
50890 |
| 1362481 |
20855 |
| 1362484 |
10053 |
| 1362485 |
22690 |
| 1362486 |
10701 |
| 1362487 |
6605 |
| 1362488 |
7083 |
| 1362489 |
55888 |
Columns to Exclude
This example demonstrates the use of the
columns=all and
columnstoexclude parameters together. Note the following output differences of the two merged input tables as compared to the previous merged table examples:
- The checking_acct table contains the per_check_fee column that is not present in the savings_acct table.
- The savings_acct table contains the acct_type column that is not present in the checking_acct table.
- Each table contains a WHERE clause limiting the output to 12 returned rows.
Call:
call td_analyze('merge','
database=
val_source,
val_source;
tablename=
checking_acct,
savings_acct;
columns=all;
columnstoexclude=
{per_check_fee},
{acct_type};
where=
{cust_id < 1362490},
{cust_id < 1362490};
');
SQL:
SELECT
"cust_id"
,"acct_nbr"
,"minimum_balance"
,"account_active"
,"acct_start_date"
,"acct_end_date"
,"starting_balance"
,"ending_balance"
FROM "val_source"."checking_acct"
WHERE cust_id < 1362490
UNION
SELECT
"cust_id"
,"acct_nbr"
,"minimum_balance"
,"account_active"
,"acct_start_date"
,"acct_end_date"
,"starting_balance"
,"ending_balance"
FROM "val_source"."savings_acct"
WHERE cust_id < 1362490
;
Results:
| cust_id |
acct_nbr |
minimum_balance |
account_active |
acct_start_date |
acct_end_date |
starting_balance |
ending_balance |
| 1362480 |
13624802 |
100 |
Y |
12/23/1992 0:00 |
|
331.85 |
86.86 |
| 1362480 |
13624803 |
100 |
Y |
12/23/1992 0:00 |
|
707.09 |
1187.97 |
| 1362484 |
13624842 |
3000 |
Y |
1/2/1993 0:00 |
|
2613.46 |
95.32 |
| 1362484 |
13624843 |
100 |
Y |
1/2/1993 0:00 |
|
194.76 |
2122.17 |
| 1362485 |
13624852 |
100 |
Y |
9/26/1995 0:00 |
|
209.78 |
45.49 |
| 1362486 |
13624862 |
3000 |
Y |
8/24/1994 0:00 |
|
4252.39 |
3312.27 |
| 1362486 |
13624863 |
100 |
Y |
7/13/1989 0:00 |
|
284.48 |
116.24 |
| 1362487 |
13624872 |
3000 |
Y |
1/12/1995 0:00 |
|
2781.07 |
1401.21 |
| 1362487 |
13624873 |
500 |
N |
6/27/1994 0:00 |
8/25/1995 0:00 651.73 |
651.73 |
12.89 |
| 1362488 |
13624882 |
3000 |
Y |
10/31/1989 0:00 |
|
4234.94 |
122.42 |
| 1362489 |
13624892 |
200 |
Y |
10/19/1993 0:00 |
|
833.4 |
125.43 |
| 1362489 |
13624893 |
100 |
Y |
9/13/1993 0:00 |
|
760.24 |
373.65 |
UNION 3 Tables
This example demonstrates the merging of three account tables: checking, credit, and savings. With
mergestyle=union, the UNION operator appears in two places in the generated SQL. The
columns=all and
columnstoexclude parameters are convenient to use because most of the columns in the three tables are the same. There are also three WHERE clauses, one for each input table, to limit the number of rows returned to only 17 rows. The parameter
addidcolumn=true includes a column MERGEID as the last column in the output table. The following values refer to which account table is indicated for MERGEID:
| Value |
Description |
| MERGEID=1 |
Checking account |
| MERGEID=2 |
Credit account |
| MERGEID=3 |
Savings account |
Call:
call td_analyze('merge','
mergestyle=union;
addidcolumn=true;
database=
val_source,
val_source,
val_source;
tablename=
checking_acct,
credit_acct,
savings_acct;
columns=all;
columnstoexclude=
{minimum_balance, per_check_fee},
{credit_limit, credit_rating},
{minimum_balance, acct_type};
where=
{cust_id < 1362490},
{cust_id < 1362490},
{cust_id < 1362490};
');
SQL:
SELECT
"cust_id"
,"acct_nbr"
,"account_active"
,"acct_start_date"
,"acct_end_date"
,"starting_balance"
,"ending_balance"
,1 AS MERGEID
FROM "val_source"."checking_acct"
WHERE cust_id < 1362490
UNION
SELECT
"cust_id"
,"acct_nbr"
,"account_active"
,"acct_start_date"
,"acct_end_date"
,"starting_balance"
,"ending_balance"
,2 AS MERGEID
FROM "val_source"."credit_acct"
WHERE cust_id < 1362490
UNION
SELECT
"cust_id"
,"acct_nbr"
,"account_active"
,"acct_start_date"
,"acct_end_date"
,"starting_balance"
,"ending_balance"
,3 AS MERGEID
FROM "val_source"."savings_acct"
WHERE cust_id < 1362490
;
Results:
| cust_id |
acct_nbr |
account_active |
acct_start_date |
acct_end_date |
starting_balance |
ending_balance |
MERGEID |
| 1362480 |
13624802 |
Y |
12/23/1992 0:00 |
|
331.85 |
86.86 |
1 |
| 1362480 |
13624803 |
Y |
12/23/1992 0:00 |
|
707.09 |
1187.97 |
3 |
| 1362480 |
4.56114E+15 |
N |
12/23/1992 0:00 |
12/15/1995 0:00 |
1607.4 |
20 |
2 |
| 1362484 |
13624842 |
Y |
1/2/1993 0:00 |
|
2613.46 |
95.32 |
1 |
| 1362484 |
13624843 |
Y |
1/2/1993 0:00 |
|
194.76 |
2122.17 |
3 |
| 1362484 |
4.56114E+15 |
Y |
1/2/1993 0:00 |
|
1278.78 |
369.72 |
2 |
| 1362485 |
13624852 |
Y |
9/26/1995 0:00 |
|
209.78 |
45.49 |
1 |
| 1362485 |
4.56114E+15 |
Y |
4/13/1992 0:00 |
|
282.38 |
1100 |
2 |
| 1362486 |
13624862 |
Y |
8/24/1994 0:00 |
|
4252.39 |
3312.27 |
1 |
| 1362486 |
13624863 |
Y |
7/13/1989 0:00 |
|
284.48 |
116.24 |
3 |
| 1362487 |
13624872 |
Y |
1/12/1995 0:00 |
|
2781.07 |
1401.21 |
1 |
| 1362487 |
13624873 |
N |
6/27/1994 0:00 |
8/25/1995 0:00 |
651.73 |
12.89 |
3 |
| 1362487 |
4.56114E+15 |
Y |
8/8/1995 0:00 |
|
0 |
1548.23 |
2 |
| 1362488 |
13624882 |
Y |
10/31/1989 0:00 |
|
4234.94 |
122.42 |
1 |
| 1362489 |
13624892 |
Y |
10/19/1993 0:00 |
|
833.4 |
125.43 |
1 |
| 1362489 |
13624893 |
Y |
9/13/1993 0:00 |
|
760.24 |
373.65 |
3 |
| 1362489 |
4.56114E+15 |
N |
8/16/1990 0:00 |
9/18/1995 0:00 |
1465.41 |
20 |
2 |
EXCEPT
In the following example, two tables – customer and customer_analysis – are merged with the EXCEPT operator. The default output style SELECT is in use and therefore the results are returned in a data set.
Call:
call td_analyze('merge','
database=
val_source,
val_source;
tablename=
customer,
customer_analysis;
columns=
{cust_id, income},
{cust_id, income};
where=
{cust_id < 1362490},
{cust_id < 1362485};
mergestyle=except;
');
SQL:
SELECT
"cust_id"
,"income" AS "income"
FROM "val_source"."customer"
WHERE cust_id < 1362490
EXCEPT
SELECT
"cust_id"
,"income" AS "income"
FROM "val_source"."customer_analysis"
WHERE cust_id < 1362485
;
Results:
| cid |
inc |
| 1362485 |
22690 |
| 1362486 |
10701 |
| 1362487 |
6605 |
| 1362488 |
7083 |
| 1362489 |
55888 |
INTERSECT
In the following example, two tables – customer and customer_analysis – are merged with the INTERSECT operator. The default output style SELECT is in use so that results are returned in a data set.
Call:
call td_analyze('merge','
database=
val_source,
val_source;
tablename=
customer,
customer_analysis;
columns=
{cust_id, income},
{cust_id, income};
where=
{cust_id < 1362490},
{cust_id < 1362485};
mergestyle=intersect;
');
SQL:
SELECT
"cust_id"
,"income" AS "income"
FROM "val_source"."customer"
WHERE cust_id < 1362490
INTERSECT
SELECT
"cust_id"
,"income" AS "income"
FROM "val_source"."customer_analysis"
WHERE cust_id < 1362485
;
Results:
| cid |
inc |
| 1362480 |
50890 |
| 1362481 |
20855 |
| 1362484 |
10053 |