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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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