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):
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:
Here is my stab at combining them, which does not work:
Here is the error I get:
The error doesn't give enough detail to track down where I'm going wrong. Any thoughts here?
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.