Dynamic query

gelen4o

New Member
Joined
Jul 31, 2017
Messages
28
Hi All,

SQL is something very new to me and I'm struggling with the following case:

There are two tables:

- "Account" (containing information for account numbers and their respective owners - "Entity")
- "Agreements" (containing information for agreements an account or entity may have) -- account or entity name also present in this table.
- One account or entity may have several agreements and one entity may own several accounts.

I have no problem running separate queries using either "Account Number" or "Entity" as KEY to extract all relevant agreements, however I would like to combine the two into just one query.

My goal is to tell SQL:

if "Account Number" not in "Agreements" then

substitute "Account Number" with "Entity" (owner)

Else:

populate agreements for "Account Number"


Thanks in advance !
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
This basically looks like two queries to me:

First, all records in Account where there is a match record in Agreements:
Code:
select t1.* 
from 
	Account t1
	inner join Agreements t2
	on t1.[Account Number] = t2.[Account Number]

Second, all records in Accounts where there is not a matching record (account number) in Agreements:
Code:
select t1.* 
from 
	Account t1
where
	t1.[Account Number] not in (select t3.[AccountNumber] from Agreements t3)


You can combine them by using UNION and thus make one query out of two:
Code:
select t1.* 
from 
	Account t1
	inner join Agreements t2
	on t1.[Account Number] = t2.[Account Number]

union all

select t1.* 
from 
	Account t1
where
	t1.[Account Number] not in (select t3.[AccountNumber] from Agreements t3)


This is untested because there is not much to test on. I would suggest some sample data if you want more explicit instructions.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top