Click here to Skip to main content
14,735,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two queries which results below data from each table

Query 1

Address_ID	Display Value	     Account_ID
1000	        10001 - Test 1	     100
1000	        10002 - Test 2	     200
1000	        10003 - Test 3	     300


Query 2

Address_ID	Display Value	   Account_ID
2000	        10001 - Test 1	   100
3000	        10004 - Test 4	   400


I need to combine these Queries and get the data as below as we see Account ID (100) do exist the same from both queries take row for that Account ID from first Query.

Address_ID	Display Value	 Account_ID
1000	      10001 - Test 1	100
1000	      10002 - Test 2	200
1000	      10003 - Test 3	300
3000	      10004 - Test 4	400


Any help is much appriciated.


What I have tried:

I tried with Union and Minus and Joins as well
Posted
Updated 10-Jul-20 9:19am

1 solution

For both SQL Server and Oracle you can use this sort of construction....
Select Address_ID, Display_Value, Account_ID
from FirstTable
union
Select Address_ID, Display_Value, Account_ID
from SecondTable
Where SecondTable.Account_ID not in (select FirstTable.Account_ID from FirstTable);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900