473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Union of Queries Stored in Variables

1
I am currently trying to recode a part of package which extracts data from a source system and processes in the datawarehouse. Basically what the code does is:

**If it is a daily processing processing (p_run_type <> 1) and p_processing_location is satisfied, distinct product ids will be inserted into v_table_name (which is basically a variable) from 3 different tables (using Union - to eliminate duplicate records)

**If it is not a daily processing (p_run_type = 1) and p_processing_location is satisfied, the same process will run as above, only, product ids will be inserted into v_table_name from 4 different tables (the same union of 3 tables above will be used plus union of 1 new table (which is only included if it is not a daily processing)

The code works fine, however, it is not well done coz given the current structure, if an update needs to be done, it should in two places - one on the first If condition (for the non daily processing) and another on the 2nd If condition (for the daily processing) which makes it very prone to errors.

The requirement is to rewrite the code in such a way that it is easy to efficiently manage updates and to reduce redundant queries.

I have two options which I am not sure if feasible at all (and considered to be best practice given the scenario). I am not a hard-core programmer and I need some help in determining the best possible option to make the code better:

OPTION A
1) Put the main query (3 tables) into a variable (named main_query) and the small query (1 table) into a variable (named sub_query)
2) If it is adjusted processing, execute main_query union sub_query
3) Else execute main_query

OPTION B

1) Run first the main query regardless of any processing day. This will insert data into v_table_name
2) If it is adjusted, run a query which is something like this

Insert into v_table_name
Select distinct product_ids
from opd_trans_product
where product_ids not in (select product_ids from v_table_name)

Below is the part of the code that needs to be modified:

IF p_run_type = 1 AND (p_processing_location = 'AMS' OR p_processing_location = 'ALL') THEN
EXECUTE IMMEDIATE
'INSERT INTO ' || v_table_name || '
SELECT DISTINCT To_Number(product_id) product_id
FROM opo_enriched_tb_extract tb
WHERE
(
((''' || p_processing_location || '''= ''PAC'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''PAC'' AND business_date = To_Date(''' || To_Char(lv_cob_date_PAC, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_PAC) || '))
OR ((''' || p_processing_location || '''= ''EUR'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''EUR'' AND business_date = To_Date(''' || To_Char(lv_cob_date_EUR, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_EUR) || '))
OR ((''' || p_processing_location || '''= ''AMS'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''AMS'' AND business_date = To_Date(''' || To_Char(lv_cob_date_AMS, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_AMS) || '))
)
AND product_id_type_cd = ''PIDC''
AND product_id IS NOT NULL
AND RTrim(product_id,'' 1234567890'') IS NULL

-- UNDERLYING
UNION
SELECT DISTINCT To_Number(product_underlying_id) product_id
FROM opo_enriched_tb_extract tb
WHERE
(
((''' || p_processing_location || '''= ''PAC'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''PAC'' AND business_date = To_Date(''' || To_Char(lv_cob_date_PAC, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_PAC) || '))
OR ((''' || p_processing_location || '''= ''EUR'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''EUR'' AND business_date = To_Date(''' || To_Char(lv_cob_date_EUR, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_EUR) || '))
OR ((''' || p_processing_location || '''= ''AMS'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''AMS'' AND business_date = To_Date(''' || To_Char(lv_cob_date_AMS, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_AMS) || '))
)
AND product_underlying_id_type_cd = ''PIDC''
AND product_underlying_id IS NOT NULL
AND RTrim(product_underlying_id,'' 1234567890'') IS NULL

-- ISSUER TB PRODUCTS
UNION
SELECT DISTINCT To_Number(ISSUER_NOTL_PRODUCT_ID) product_id
FROM OPO_ENRICHED_ISSUER_TB tb
WHERE
(
((''' || p_processing_location || '''= ''PAC'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''PAC'' AND business_date = To_Date(''' || To_Char(lv_cob_date_PAC, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_PAC) || '))
OR ((''' || p_processing_location || '''= ''EUR'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''EUR'' AND business_date = To_Date(''' || To_Char(lv_cob_date_EUR, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_EUR) || '))
OR ((''' || p_processing_location || '''= ''AMS'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''AMS'' AND business_date = To_Date(''' || To_Char(lv_cob_date_AMS, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_AMS) || '))
)
AND ISSUER_NOTL_PRODUCT_ID_TYPE_CD = ''PIDC''
AND ISSUER_NOTL_PRODUCT_ID IS NOT NULL
AND RTrim(ISSUER_NOTL_PRODUCT_ID,'' 1234567890'') IS NULL

-- POSITION FILES RECORDS
UNION
SELECT DISTINCT To_Number(p_id_c) product_id
FROM insight_security tb
WHERE ref_date = To_Date(''' || To_Char(lv_cob_date, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'')
AND p_id_c IS NOT NULL
AND RTrim(p_id_c,'' 1234567890'') IS NULL

-- TICS TRANS PROD RECORDS
UNION
SELECT DISTINCT To_Number(product_id) product_id
FROM opd_trans_product tp
WHERE
(
((''' || p_processing_location || '''= ''AMS'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''AMS'' AND business_date = To_Date(''' || To_Char(lv_cob_date_AMS, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_AMS) || '))
)
AND allocation_id_type_cd = ''TICS''
AND product_id IS NOT NULL
AND RTrim(product_id,'' 1234567890'') IS NULL';

ELSE
EXECUTE IMMEDIATE
'INSERT INTO ' || v_table_name || '

SELECT DISTINCT To_Number(product_id) product_id
FROM opo_enriched_tb_extract tb
WHERE
(
((''' || p_processing_location || '''= ''PAC'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''PAC'' AND business_date = To_Date(''' || To_Char(lv_cob_date_PAC, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_PAC) || '))
OR ((''' || p_processing_location || '''= ''EUR'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''EUR'' AND business_date = To_Date(''' || To_Char(lv_cob_date_EUR, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_EUR) || '))
OR ((''' || p_processing_location || '''= ''AMS'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''AMS'' AND business_date = To_Date(''' || To_Char(lv_cob_date_AMS, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_AMS) || '))
)
AND product_id_type_cd = ''PIDC''
AND product_id IS NOT NULL
AND RTrim(product_id,'' 1234567890'') IS NULL

-- UNDERLYING
UNION
SELECT DISTINCT To_Number(product_underlying_id) product_id
FROM opo_enriched_tb_extract tb
WHERE
(
((''' || p_processing_location || '''= ''PAC'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''PAC'' AND business_date = To_Date(''' || To_Char(lv_cob_date_PAC, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_PAC) || '))
OR ((''' || p_processing_location || '''= ''EUR'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''EUR'' AND business_date = To_Date(''' || To_Char(lv_cob_date_EUR, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_EUR) || '))
OR ((''' || p_processing_location || '''= ''AMS'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''AMS'' AND business_date = To_Date(''' || To_Char(lv_cob_date_AMS, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_AMS) || '))
)
AND product_underlying_id_type_cd = ''PIDC''
AND product_underlying_id IS NOT NULL
AND RTrim(product_underlying_id,'' 1234567890'') IS NULL

-- ISSUER TB PRODUCTS
UNION
SELECT DISTINCT To_Number(ISSUER_NOTL_PRODUCT_ID) product_id
FROM OPO_ENRICHED_ISSUER_TB tb
WHERE
(
((''' || p_processing_location || '''= ''PAC'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''PAC'' AND business_date = To_Date(''' || To_Char(lv_cob_date_PAC, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_PAC) || '))
OR ((''' || p_processing_location || '''= ''EUR'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''EUR'' AND business_date = To_Date(''' || To_Char(lv_cob_date_EUR, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_EUR) || '))
OR ((''' || p_processing_location || '''= ''AMS'' OR ''' || p_processing_location || '''= ''ALL'') AND (processing_location = ''AMS'' AND business_date = To_Date(''' || To_Char(lv_cob_date_AMS, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'') AND run_id = ' || To_Char(lv_run_id_AMS) || '))
)
AND ISSUER_NOTL_PRODUCT_ID_TYPE_CD = ''PIDC''
AND ISSUER_NOTL_PRODUCT_ID IS NOT NULL
AND RTrim(ISSUER_NOTL_PRODUCT_ID,'' 1234567890'') IS NULL

-- POSITION FILES RECORDS
UNION
SELECT DISTINCT To_Number(p_id_c) product_id
FROM insight_security tb
WHERE ref_date = To_Date(''' || To_Char(lv_cob_date, 'MM.DD.YYYY') || ''', ''MM.DD.YYYY'')
AND p_id_c IS NOT NULL
AND RTrim(p_id_c,'' 1234567890'') IS NULL';

END IF;
COMMIT;

I am hoping that you could help me out on this.
May 13 '10 #1
0 1162

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Brent Wege | last post by:
Hi I have the following union query that retrieves two counts. Can I sum them up within this query, like wrap this in a sum function somehow to get the total count? Or is there a better way to do...
2
by: d2r2 | last post by:
Hi, I'm trying to run a nested (UNION) query against a MSAccessXP database (version 2002; build 10.6501.6714; SP3) In Access the SQL-statement executes just fine. When I run it in a asp-page I...
5
by: ahokdac-sql | last post by:
Hi, I'm adapting access queries to sql server and I have difficulties with the following pattern : query1 : SELECT * FROM Query2 WHERE A=@param1 query 2: SELECT * FROM Table2 WHERE B=@param2 ...
2
by: Fred Zuckerman | last post by:
I have a union query joining two other queries, each with 62 fields. Interestingly, when I open the query it has the correct number of 2850 records. But if I then set a filter (using filter by...
1
by: Fl?vio | last post by:
Hey! Please help me... I'd like to know if there's a limit of tables that I can unite in a union query (maybe 16?). When I add more than 16 tables I'm getting an error message. Thank you in...
2
by: Aaron Haspel | last post by:
Greetings Access gurus. I have clients with Access databases in the field that I need to update -- new tables, new indices, new queries, the works. Since these clients may have only the Access...
3
by: Paul H | last post by:
I have been building access databases for a few years now and have never built a union or pass through query. What are these strange mythical beasts? What do they do? Paul
0
by: tdotsmiley | last post by:
Hi, I have multiple union queries that I have. These are from 4 different tables. Is there a way I can use the report feature so that it combines all of the queries into one file so that I can...
5
by: BillCo | last post by:
I've encountered a problem while using ADO to save query objects. Union queries created normally (via the interface) appear in adox catelog.procedures rather than catelog.views. This is reasonably...
2
by: MLH | last post by:
Consider having tblCorrespondence, then copying & pasting it to tblCorrespondence1 - such that they are identical. Then consider the following UNION SELECT statement... SELECT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.