Dreaded Nested Query, 4 Select Statements

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
I have a query with nested selections. The issue is, I need to replace the reference to one of the tables in the nested query to another query which obtains it's data from a saved query in Access.

Can someone with more experience in nesting queries take a look and let me know where I'm going wrong?

Here is a breakdown, including my attempt (which results in an error). The data below should help clarify and demonstrate the problem I have. Thanks!

Existing nested query (works fine):
Code:
SELECT 
       Q1.TimeDay, 
   Sum(Q1.SalesProd1) AS SumOfSalesProd1, 
   Sum(Q1.DailyProd2) AS SumOfDailyProd2, 
   Sum(Q1.DailyProd3) AS SumOfDailyProd3, 
 Count(Q1.Item_KEY)   AS CountOfItem_KEY
FROM 
        (SELECT 
         DailyReadings.Item_KEY, 
         DailyReadings.ReadingDate, 
         DailyReadings.SalesProd1,
         DailyReadings.DailyProd2, 
         DailyReadings.DailyProd3,
               (SELECT 
               COUNT(Table1A.ReadingDate) 
               FROM [DailyReadings] AS Table1A 
               WHERE [Table1A].[ReadingDate]<=[DailyReadings].[ReadingDate] AND [Table1A].[Item_KEY]=[DailyReadings].[Item_KEY]) AS TimeDay
         FROM tblProperties INNER JOIN Q2 ON tblProperties.WH_IDX = Q2.Item_KEY 
         WHERE (((tblProperties.PROP_GROUP)="MY_GROUP_SELECTION") AND ((tblProperties.PROP_AREA)="MY_AREA")) ORDER BY Q2.ReadingDate) AS Q1
GROUP BY Q1.TimeDay;

The table "DailyReadings" above is deprecated. Instead, the query above needs to reference a query which has the same fields.

We'll call this query "Q2". Here is the code, which works fine on it's own:
Code:
SELECT
fd_sysSite.SiteID AS Item_KEY,
[_Group_Products].Date AS ReadingDate, 
[_Group_Products].SumOfProd1 AS SalesProd1,
[_Group_Products].DailyProd2, 
[_Group_Products].DailyProd3
FROM fd_sysSite
INNER JOIN [_Group_Products] ON fd_sysSite.SiteID = [_Group_Products].SiteID

Here is my stab at combining them, which does not work:

Code:
SELECT 
          Q1.TimeDay, 
   Sum(Q1.SalesProd1) AS SumOfSalesProd1, 
   Sum(Q1.DailyProd2) AS SumOfDailyProd2, 
   Sum(Q1.DailyProd3) AS SumOfDailyProd3, 
  Count(Q1.Item_KEY)  AS CountOfItem_KEY
FROM 
        (SELECT 
         Q2.Item_KEY, 
         Q2.ReadingDate, 
         Q2.SalesProd1,
         Q2.DailyProd2, 
         Q2.DailyProd3,
               (SELECT 
               COUNT(Q2A.ReadingDate) 
               FROM [Q2] AS Q2A  
               WHERE [Q2A].[ReadingDate]<=[Q2].[ReadingDate] AND [Q2A].[Item_KEY]=[Q2].[Item_KEY]) AS TimeDay
                                 (SELECT
                                  fd_sysSite.SiteID AS Item_KEY,
                                  [_Group_Products].[Date] AS ReadingDate, 
                                  [_Group_Products].SumOfProd1 AS SalesProd1,
                                  [_Group_Products].DailyProd2, 
                                  [_Group_Products].DailyProd3
                                  FROM fd_sysSite
                                  INNER JOIN [_Group_Products] ON fd_sysSite.SiteID = [_Group_Products].SiteID) AS Q2
         FROM tblProperties INNER JOIN Q2 ON tblProperties.WH_IDX = Q2.Item_KEY 
         WHERE (((tblProperties.PROP_GROUP)="MY_GROUP_SELECTION") AND ((tblProperties.PROP_AREA)="MY_AREA")) ORDER BY Q2.ReadingDate) AS Q1
GROUP BY Q1.TimeDay;

Here is the error I get:
Code:
The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.
The error doesn't give enough detail to track down where I'm going wrong. Any thoughts here?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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