Voltron
Defender of the universe
- Local time
- Today, 12:02
- Joined
- Jul 9, 2009
- Messages
- 77
I have a relatively complex query that I have built (it is pulling information for calculated fields from roughly 30 other queries that were created to make calculated fields) and one thing is really causing me problems.
We have clients, but some clients are split into 2 clients (Client and Client - Nevada). We are trying to come up with the appropriate fees, which is partly known and partly calculated. The formula is as follows:
(($1000*4)*(Total Plan Assets/(Total Plan Assets+Total Plan Assets of the second client)))
Total Plan Assets is a calculated field that I have stored in a query.
The pertinent table and query are as follows:
Clients
Client_ID
Consulting_Fee (this is a hard dollar fee, like the $1000 in the formula)
Split (this is a yes or no check box to determine if the client is split into 2)
ClientsWithTotalAssetsQuery
Client_ID (creates the Inner Join)
Total_Assets
To do the formula in one query it seems like a subquery would be the way to go (which vbaInet has also confirmed in a previous thread). However, I am having problems with this subquery. I have done subqueries before, but not to solve a problem quite like this.
It seems like I should have a subquery in the Select section of the base query.
Here is kind of what I was thinking:
It has been a while since I have written SQL from scratch so this may not be all correct, but this is what I was thinking of. The problem I am having is I am not sure exactly how to execute my WHERE in the subquery properly. I realize that what I have won't work, so is there any way to do what I am trying to do?
Also, is this even anywhere close to something that would be re-workable to be usable?
We have clients, but some clients are split into 2 clients (Client and Client - Nevada). We are trying to come up with the appropriate fees, which is partly known and partly calculated. The formula is as follows:
(($1000*4)*(Total Plan Assets/(Total Plan Assets+Total Plan Assets of the second client)))
Total Plan Assets is a calculated field that I have stored in a query.
The pertinent table and query are as follows:
Clients
Client_ID
Consulting_Fee (this is a hard dollar fee, like the $1000 in the formula)
Split (this is a yes or no check box to determine if the client is split into 2)
ClientsWithTotalAssetsQuery
Client_ID (creates the Inner Join)
Total_Assets
To do the formula in one query it seems like a subquery would be the way to go (which vbaInet has also confirmed in a previous thread). However, I am having problems with this subquery. I have done subqueries before, but not to solve a problem quite like this.
It seems like I should have a subquery in the Select section of the base query.
Here is kind of what I was thinking:
Code:
SELECT Clients.Client_ID, Clients.Split, Clients.Consulting_Fee,
(SELECT Clients.Client_ID AS Client_ID_2, ClientsWithTotalAssetsQuery.Total_Assets AS Total_Assets_Client_2
FROM Clients INNER JOIN [ClientsWithTotalAssets Query] ON Clients.Client_ID = [ClientsWithTotal AssetsQuery].Client_ID
WHERE Client_ID_2 LIKE *Clients.Client_ID*),
((Clients.Consulting_Fee*4)/(ClientsWithTotalAssetsQuery.Total_Assets/(ClientsWithTotalAssetsQuery.Total_Assets+Total_Assets_Client_2))) AS FeeAmount
FROM Clients INNER JOIN [ClientsWithTotalAssetsQuery] ON Clients.Client_ID = [ClientsWithTotalAssetsQuery].Client_ID
WHERE Clients.Split = Yes
It has been a while since I have written SQL from scratch so this may not be all correct, but this is what I was thinking of. The problem I am having is I am not sure exactly how to execute my WHERE in the subquery properly. I realize that what I have won't work, so is there any way to do what I am trying to do?
Also, is this even anywhere close to something that would be re-workable to be usable?
Last edited: