473,569 Members | 2,984 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Subquery Help Needed

2 New Member
I'm new to this group and not an experienced Access user, but have solved several of my problems already by reading other threads on this forum (Thanks!). Here's my current problem that I can't figure out how to solve:

I'm trying to calculate total project management budget by project phase. I have a query that returns data grouped by project and index code, and has the sum of the PM budgets along with a field called 'WBS_CONCAT_Nam e'. This name field specifies which phase of the project the budget belongs to (Design, Construction, Bid & Award, etc.). I now need to group and sum by that concatenated name field in order to get the PM budget by phase. Here's the query that extracts the project management budget:

SELECT ADMUSER_PUC_PRO J_MONTHLY_REPOR T.PROJ_ID, ADMUSER_PUC_PRO J_MONTHLY_REPOR T.PROJ_SHORT_NA ME, ADMUSER_PUC_PRO J_MONTHLY_REPOR T.PROJ_NAME, ADMUSER_PUC_WBS _BUDGETED_COSTS .WBS_CONCAT_NAM E, Sum(ADMUSER_PUC _WBS_BUDGETED_C OSTS.ORIG_BUDGE TED_COST) AS [PM Budget]
FROM ADMUSER_PUC_PRO J_MONTHLY_REPOR T INNER JOIN ADMUSER_PUC_WBS _BUDGETED_COSTS ON ADMUSER_PUC_PRO J_MONTHLY_REPOR T.PROJ_ID = ADMUSER_PUC_WBS _BUDGETED_COSTS .PROJ_ID
WHERE (((ADMUSER_PUC_ WBS_BUDGETED_CO STS.WBS_NAME)=" Project Management"))
GROUP BY ADMUSER_PUC_PRO J_MONTHLY_REPOR T.PROJ_ID, ADMUSER_PUC_PRO J_MONTHLY_REPOR T.PROJ_SHORT_NA ME, ADMUSER_PUC_PRO J_MONTHLY_REPOR T.PROJ_NAME, ADMUSER_PUC_WBS _BUDGETED_COSTS .WBS_CONCAT_NAM E;

The data in the field 'WBS_CONCAT_NAM E' looks like this:
CUW37801.BA.PM
CUW37801.CL.PM
CUW37801.CM.PM
CUW37801.DS.PM
CUW37801.ER.PM
CUW37801.PL.PM
CUW37901.BA.PM
CUW37901.CL.PM
CUW37901.CM.PM
CUW37901.DS.PM
CUW37901.ER.PM
CUW37901.PL.PM
CUW37201.BA.PM

I'm trying to write a subquery that looks at this string and sums all the budgets where the string contains 'BA' into a total called Bid&AwardPMBudg et, CL into closeout budget, etc. Here's what I tried:

SELECT [B - P3e PM Budgets].PROJ_ID, [B - P3e PM Budgets].PROJ_SHORT_NAM E, [B - P3e PM Budgets].PROJ_NAME, Sum((SELECT [PM Budget] FROM [B - P3e PM Budgets] WHERE ((([B - P3e PM Budgets].WBS_CONCAT_NAM E) Like '*DS*')))) AS [PM Budget]
FROM [B - P3e PM Budgets]
GROUP BY [B - P3e PM Budgets].PROJ_ID, [B - P3e PM Budgets].PROJ_SHORT_NAM E, [B - P3e PM Budgets].PROJ_NAME;

I get a message saying this query can return only one record.

I think my problem is that I can't just use a subquery and instead need to make a temp table? I have no idea if that will work or how to do it, and I also have no idea if there's a way to do this in the initial query. I didn't find info in help, or on the Microsoft website and would appreciate any advice. Thanks.
Nov 1 '06 #1
2 1465
MMcCarthy
14,534 Recognized Expert Moderator MVP
Try something like this:

SELECT Sum(IIf([B - P3e PM Budgets].[WBS_CONCAT_NAME] Like '*BA*',[B - P3e PM Budgets].[PM Budget],0)) As Bid&AwardPMBudg et,
Sum(IIf([B - P3e PM Budgets].[WBS_CONCAT_NAME] Like '*CL*',[B - P3e PM Budgets].[PM Budget],0)) As CloseoutBudget
FROM [B - P3e PM Budgets];

When you include other fields in your query the sum total you are trying to achieve is being broken down by those fields.
Nov 1 '06 #2
dcourington
2 New Member
Try something like this:

SELECT Sum(IIf([B - P3e PM Budgets].[WBS_CONCAT_NAME] Like '*BA*',[B - P3e PM Budgets].[PM Budget],0)) As Bid&AwardPMBudg et,
Sum(IIf([B - P3e PM Budgets].[WBS_CONCAT_NAME] Like '*CL*',[B - P3e PM Budgets].[PM Budget],0)) As CloseoutBudget
FROM [B - P3e PM Budgets];

When you include other fields in your query the sum total you are trying to achieve is being broken down by those fields.
THANK YOU!!!! I didn't even think of trying IIF but it worked perfectly.
Nov 1 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4566
by: lev | last post by:
CREATE TABLE . ( NULL , , (44) ) ID is non-unique. I want to select all IDs where the last entry for that ID is of type 11.
7
9194
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into a table (c) re-queries another table using a subquery which references the inserted table (correlated or not)
0
563
by: Greg Stark | last post by:
Postgresql 7.4b2 (approximately, compiled out of CVS) When I have a subquery that has a complex subquery as one of the result columns, and then that result column is used multiple times in the parent query, the subquery is inlined for each one. This means multiple redundant executions of the subquery. I recall there was a way to defeat...
8
5090
by: Venkata C | last post by:
Hi! Does anyone here know of a way to goad DB2 into converting a correlated subquery to a non-correlated one? Does DB2 ever do such a conversion? We have a query of the form SELECT .. FROM A WHERE EXISTS (SELECT 'X' FROM B, C WHERE B.COL1 = A.COL1 AND B.COL2 = A.COL2
5
4232
by: Mike MacSween | last post by:
This as the row source for a combo: SELECT qryRole.RoleID, qryRole.Role FROM qryRole WHERE (((qryRole.RoleID) Not In (SELECT RoleID FROM qryRoleEvent INNER JOIN qryEvent ON qryRoleEvent.EventID = qryEvent.EventID WHERE ProdID = Forms!frmProductions!ProdID))) ORDER BY qryRole.Role; If there is just one RoleID with a null value in the...
2
1395
by: Hexman | last post by:
Don't know if this is the proper newsgroup to post to. Sorry if not. Can't figure this one out. Using Access 2003. I have 2 tables, IMast (table of part info) and THist (part transaction history). I need to select all parts that have a product code = "CAST" and list the last 10 history transaction if they are within 90 days of the run...
2
1206
by: BerkshireGuy | last post by:
tblDetails has the following fields: AgencyCode, ProducerCode, AnnualPX How can I create a query to show the Top 5 producers with the highest PX per agency? Can a subquery do it? TIA
5
8016
by: Anne | last post by:
Hello! Here is the statement in question: --STATEMENT A SELECT * FROM dbo.myTable WHERE colX in (SELECT colX FROM dbo.sourceTable) The problem with Statement A is that 'colX' does not exist in 'dbo.sourceTable'. It does, however, certainly exist in 'dbo.myTable'. Breaking the statement down, we have:
1
4144
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only allows me to filter based on variables in a single table. I would like to have a search form where I can select multiple variables (from various...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.