Last active
January 23, 2025 18:28
-
-
Save lalitsingh24x7/6382150e980f0a62df62e5e12cb82871 to your computer and use it in GitHub Desktop.
union.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE VIEW Combined_Portfolio AS | |
| SELECT | |
| portfolio_id, | |
| amount, | |
| 'Portfolio_two' AS source_table | |
| FROM | |
| Portfolio_two | |
| UNION ALL | |
| SELECT | |
| t1.portfolio_id, | |
| t1.amount, | |
| 'Portfolio_one' AS source_table | |
| FROM | |
| Portfolio_one t1 | |
| WHERE | |
| NOT EXISTS ( | |
| SELECT 1 | |
| FROM Portfolio_two t2 | |
| WHERE t2.portfolio_id = t1.portfolio_id | |
| ); | |
| ---------------- | |
| WITH Combined_Portfolio AS ( | |
| SELECT | |
| portfolio_id, | |
| amount, | |
| asofdate, | |
| 'Portfolio_two' AS source_table | |
| FROM | |
| Portfolio_two t2 | |
| WHERE NOT EXISTS ( | |
| SELECT 1 | |
| FROM Portfolio_one t1 | |
| WHERE t1.portfolio_id = t2.portfolio_id | |
| AND t1.asofdate = t2.asofdate | |
| ) | |
| UNION ALL | |
| SELECT | |
| t1.portfolio_id, | |
| t1.amount, | |
| t1.asofdate, | |
| 'Portfolio_one' AS source_table | |
| FROM | |
| Portfolio_one t1 | |
| ) | |
| SELECT | |
| * | |
| FROM | |
| Combined_Portfolio | |
| ; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment