A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

SubQueries



 
 
Thread Tools Display Modes
  #1  
Old March 19th, 2010, 11:19 AM posted to microsoft.public.access.queries
Bruce
external usenet poster
 
Posts: 374
Default SubQueries

I am running Access 2007 SP2 on Windows XP Professional SP 3. I have a query
that I need to run a subquery to obtain a value in the previous record.
Basically I have a query that contains mileages grouped by EquipmentID and
month. I need to calculate miles driven in a given month. I need the last
mileage reading in the previous months record to run the calculation on the
month that has focus. I know that I have to alias the query, but that is
where my memory ends. I have done this before with help from this site and
hope to be as fortunate again.
--
Bruce
  #2  
Old March 19th, 2010, 02:50 PM posted to microsoft.public.access.queries
KARL DEWEY
external usenet poster
 
Posts: 10,767
Default SubQueries

Try this --
SELECT EquipmentID, Odometer, TripDate, (SELECT YourTable.Odometer -
[XX].Odometer FROM YourTable AS [XX] WHERE Max([XX].Odometer)
YourTable.Odometer ORDER BY [XX].Odometer DEC) AS TripMileage
FROM YourTable
ORDER BY YourTable.Odometer;

--
Build a little, test a little.


"Bruce" wrote:

I am running Access 2007 SP2 on Windows XP Professional SP 3. I have a query
that I need to run a subquery to obtain a value in the previous record.
Basically I have a query that contains mileages grouped by EquipmentID and
month. I need to calculate miles driven in a given month. I need the last
mileage reading in the previous months record to run the calculation on the
month that has focus. I know that I have to alias the query, but that is
where my memory ends. I have done this before with help from this site and
hope to be as fortunate again.
--
Bruce

  #3  
Old March 19th, 2010, 02:52 PM posted to microsoft.public.access.queries
KARL DEWEY
external usenet poster
 
Posts: 10,767
Default SubQueries

I left out the EquipmentID from the subquery --
SELECT EquipmentID, Odometer, TripDate, (SELECT YourTable.Odometer -
[XX].Odometer FROM YourTable AS [XX] WHERE [XX].EquipmentID =
YourTable.EquipmentID AND Max([XX].Odometer) YourTable.Odometer ORDER BY
[XX].Odometer DEC) AS TripMileage
FROM YourTable
ORDER BY YourTable.Odometer;

--
Build a little, test a little.


"Bruce" wrote:

I am running Access 2007 SP2 on Windows XP Professional SP 3. I have a query
that I need to run a subquery to obtain a value in the previous record.
Basically I have a query that contains mileages grouped by EquipmentID and
month. I need to calculate miles driven in a given month. I need the last
mileage reading in the previous months record to run the calculation on the
month that has focus. I know that I have to alias the query, but that is
where my memory ends. I have done this before with help from this site and
hope to be as fortunate again.
--
Bruce

  #4  
Old March 19th, 2010, 04:45 PM posted to microsoft.public.access.queries
Bruce
external usenet poster
 
Posts: 374
Default SubQueries

Karl,

Thank you for your reply. I keep getting the following error message:

Syntax error (missing operator) in query expression ‘(Select Top 1
[Dupe].Mileage FROM [Monthly Miles Master FD 03] AS [Dupe] WHERE
[Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID] AND
[Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]) ORDER BY
[Dupe].[YearMonth’.

This is the entire string:

SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage, (SELECT
TOP 1 [Dupe].Mileage FROM [Monthly Miles from Master FD 03] AS [Dupe] WHERE
[Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID] AND
[Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]) ORDER BY
[Dupe].[YearMonth] DESC AS PreviousMileage
FROM [Monthly Miles from Master FD 03];

I have tried several different configurations to no avail; can you tell me
what I am doing wrong??

--
Bruce


"KARL DEWEY" wrote:

I left out the EquipmentID from the subquery --
SELECT EquipmentID, Odometer, TripDate, (SELECT YourTable.Odometer -
[XX].Odometer FROM YourTable AS [XX] WHERE [XX].EquipmentID =
YourTable.EquipmentID AND Max([XX].Odometer) YourTable.Odometer ORDER BY
[XX].Odometer DEC) AS TripMileage
FROM YourTable
ORDER BY YourTable.Odometer;

--
Build a little, test a little.


"Bruce" wrote:

I am running Access 2007 SP2 on Windows XP Professional SP 3. I have a query
that I need to run a subquery to obtain a value in the previous record.
Basically I have a query that contains mileages grouped by EquipmentID and
month. I need to calculate miles driven in a given month. I need the last
mileage reading in the previous months record to run the calculation on the
month that has focus. I know that I have to alias the query, but that is
where my memory ends. I have done this before with help from this site and
hope to be as fortunate again.
--
Bruce

  #5  
Old March 19th, 2010, 05:21 PM posted to microsoft.public.access.queries
Bruce
external usenet poster
 
Posts: 374
Default SubQueries

Karl,

I removed some brackets [] from the field names and it wanted to run but now
I am getting the following error message:

Unknown Access database engine error

Here is the current code:

SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage, (SELECT
TOP 1 [Dupe].Mileage FROM [Monthly Miles from Master FD 03] AS [Dupe] WHERE
[Dupe].EquipmentID = [Monthly Miles from Master FD 03].EquipmentID AND
[Dupe].YearMonth [Monthly Miles from Master FD 03].YearMonth ORDER BY
[Dupe].[YearMonth] DESC) AS PreviousMileage
FROM [Monthly Miles from Master FD 03];

I think it may be due to the fact that I have not provide an IIf statement
when a null condition exists (like there is no previous record, the one it is
on is the first record)

Bruce


"KARL DEWEY" wrote:

I left out the EquipmentID from the subquery --
SELECT EquipmentID, Odometer, TripDate, (SELECT YourTable.Odometer -
[XX].Odometer FROM YourTable AS [XX] WHERE [XX].EquipmentID =
YourTable.EquipmentID AND Max([XX].Odometer) YourTable.Odometer ORDER BY
[XX].Odometer DEC) AS TripMileage
FROM YourTable
ORDER BY YourTable.Odometer;

--
Build a little, test a little.


"Bruce" wrote:

I am running Access 2007 SP2 on Windows XP Professional SP 3. I have a query
that I need to run a subquery to obtain a value in the previous record.
Basically I have a query that contains mileages grouped by EquipmentID and
month. I need to calculate miles driven in a given month. I need the last
mileage reading in the previous months record to run the calculation on the
month that has focus. I know that I have to alias the query, but that is
where my memory ends. I have done this before with help from this site and
hope to be as fortunate again.
--
Bruce

  #6  
Old March 19th, 2010, 05:38 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default SubQueries

I would try something along the following lines

To fix the syntax try removing the parenthesis Before the ORDER BY in the
sub-query and adding a closing parenthesis after DESC.

SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage
, (SELECT TOP 1 [Dupe].Mileage
FROM [Monthly Miles from Master FD 03] AS [Dupe]
WHERE [Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID]
AND [Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]
ORDER BY [Dupe].[YearMonth] DESC) AS PreviousMileage

FROM [Monthly Miles from Master FD 03];

That may still error. I would try something like the following.

SELECT [Monthly Miles from Master FD 03].EquipmentID
, [Monthly Miles from Master FD 03].YearMonth
, [Monthly Miles from Master FD 03].Mileage

, (SELECT Max([Dupe].Mileage)
FROM [Monthly Miles from Master FD 03] AS [Dupe]
WHERE [Dupe].[EquipmentID] =
[Monthly Miles from Master FD 03].[EquipmentID]
AND Dupe.Mileage [Monthly Miles from Master FD 03].[Mileage]) AS
PreviousMileage

FROM [Monthly Miles from Master FD 03];

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Bruce wrote:
Karl,

Thank you for your reply. I keep getting the following error message:

Syntax error (missing operator) in query expression ‘(Select Top 1
[Dupe].Mileage FROM [Monthly Miles Master FD 03] AS [Dupe] WHERE
[Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID] AND
[Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]) ORDER BY
[Dupe].[YearMonth’.

This is the entire string:

SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage, (SELECT
TOP 1 [Dupe].Mileage FROM [Monthly Miles from Master FD 03] AS [Dupe] WHERE
[Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID] AND
[Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]) ORDER BY
[Dupe].[YearMonth] DESC AS PreviousMileage
FROM [Monthly Miles from Master FD 03];

I have tried several different configurations to no avail; can you tell me
what I am doing wrong??

  #7  
Old March 19th, 2010, 07:13 PM posted to microsoft.public.access.queries
Bruce
external usenet poster
 
Posts: 374
Default SubQueries

John,

I removed all brackets [] from the field names and it wanted to run but now
I am getting the following error message:

Unknown Access database engine error

I dont get this error until the query has run for a period of time and I get
it just when I thought it would display the results. Could it be from the
fact that I am not checking for nulls?

--
Bruce


"John Spencer" wrote:

I would try something along the following lines

To fix the syntax try removing the parenthesis Before the ORDER BY in the
sub-query and adding a closing parenthesis after DESC.

SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage
, (SELECT TOP 1 [Dupe].Mileage
FROM [Monthly Miles from Master FD 03] AS [Dupe]
WHERE [Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID]
AND [Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]
ORDER BY [Dupe].[YearMonth] DESC) AS PreviousMileage

FROM [Monthly Miles from Master FD 03];

That may still error. I would try something like the following.

SELECT [Monthly Miles from Master FD 03].EquipmentID
, [Monthly Miles from Master FD 03].YearMonth
, [Monthly Miles from Master FD 03].Mileage

, (SELECT Max([Dupe].Mileage)
FROM [Monthly Miles from Master FD 03] AS [Dupe]
WHERE [Dupe].[EquipmentID] =
[Monthly Miles from Master FD 03].[EquipmentID]
AND Dupe.Mileage [Monthly Miles from Master FD 03].[Mileage]) AS
PreviousMileage

FROM [Monthly Miles from Master FD 03];

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Bruce wrote:
Karl,

Thank you for your reply. I keep getting the following error message:

Syntax error (missing operator) in query expression ‘(Select Top 1
[Dupe].Mileage FROM [Monthly Miles Master FD 03] AS [Dupe] WHERE
[Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID] AND
[Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]) ORDER BY
[Dupe].[YearMonth’.

This is the entire string:

SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage, (SELECT
TOP 1 [Dupe].Mileage FROM [Monthly Miles from Master FD 03] AS [Dupe] WHERE
[Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID] AND
[Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]) ORDER BY
[Dupe].[YearMonth] DESC AS PreviousMileage
FROM [Monthly Miles from Master FD 03];

I have tried several different configurations to no avail; can you tell me
what I am doing wrong??

.

  #8  
Old March 19th, 2010, 09:00 PM posted to microsoft.public.access.queries
Bruce
external usenet poster
 
Posts: 374
Default SubQueries

John,

I got the query to run. However the query appears to be stuck in a loop. I
limited the selection to UD793, there should have been 26 records. There were
many interations of 26 records, I suspect there would have been at least 650
records. How do I code the query so it only runs once and does not keep
accessing the same records? Here is code that I used to get the query to run:
(I had to add the table and alias in the properties window)

SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage, (SELECT
TOP 1 [Dupe].Mileage FROM [Monthly Miles from Master FD 03] AS [Dupe] WHERE
[Dupe].EquipmentID = [Monthly Miles from Master FD 03].EquipmentID AND
[Dupe].YearMonth [Monthly Miles from Master FD 03].YearMonth ORDER BY
[Dupe].[YearMonth] DESC) AS PreviousMileage
FROM [Monthly Miles from Master FD 03], [Monthly Miles from Master FD 03] AS
Dupe
WHERE (([Monthly Miles from Master FD 03].EquipmentID)="UD793");
--
Bruce


"John Spencer" wrote:

I would try something along the following lines

To fix the syntax try removing the parenthesis Before the ORDER BY in the
sub-query and adding a closing parenthesis after DESC.

SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage
, (SELECT TOP 1 [Dupe].Mileage
FROM [Monthly Miles from Master FD 03] AS [Dupe]
WHERE [Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID]
AND [Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]
ORDER BY [Dupe].[YearMonth] DESC) AS PreviousMileage

FROM [Monthly Miles from Master FD 03];

That may still error. I would try something like the following.

SELECT [Monthly Miles from Master FD 03].EquipmentID
, [Monthly Miles from Master FD 03].YearMonth
, [Monthly Miles from Master FD 03].Mileage

, (SELECT Max([Dupe].Mileage)
FROM [Monthly Miles from Master FD 03] AS [Dupe]
WHERE [Dupe].[EquipmentID] =
[Monthly Miles from Master FD 03].[EquipmentID]
AND Dupe.Mileage [Monthly Miles from Master FD 03].[Mileage]) AS
PreviousMileage

FROM [Monthly Miles from Master FD 03];

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Bruce wrote:
Karl,

Thank you for your reply. I keep getting the following error message:

Syntax error (missing operator) in query expression ‘(Select Top 1
[Dupe].Mileage FROM [Monthly Miles Master FD 03] AS [Dupe] WHERE
[Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID] AND
[Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]) ORDER BY
[Dupe].[YearMonth’.

This is the entire string:

SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage, (SELECT
TOP 1 [Dupe].Mileage FROM [Monthly Miles from Master FD 03] AS [Dupe] WHERE
[Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID] AND
[Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]) ORDER BY
[Dupe].[YearMonth] DESC AS PreviousMileage
FROM [Monthly Miles from Master FD 03];

I have tried several different configurations to no avail; can you tell me
what I am doing wrong??

.

  #9  
Old March 19th, 2010, 09:00 PM posted to microsoft.public.access.queries
Bruce
external usenet poster
 
Posts: 374
Default SubQueries

Karl,

I got the query to run. However the query appears to be stuck in a loop. I
limited the selection to UD793, there should have been 26 records. There were
many interations of 26 records, I suspect there would have been at least 650
records. How do I code the query so it only runs once and does not keep
accessing the same records? Here is code that I used to get the query to run:
(I had to add the table and alias in the properties window)

SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage, (SELECT
TOP 1 [Dupe].Mileage FROM [Monthly Miles from Master FD 03] AS [Dupe] WHERE
[Dupe].EquipmentID = [Monthly Miles from Master FD 03].EquipmentID AND
[Dupe].YearMonth [Monthly Miles from Master FD 03].YearMonth ORDER BY
[Dupe].[YearMonth] DESC) AS PreviousMileage
FROM [Monthly Miles from Master FD 03], [Monthly Miles from Master FD 03] AS
Dupe
WHERE (([Monthly Miles from Master FD 03].EquipmentID)="UD793");
--
Bruce


"KARL DEWEY" wrote:

I left out the EquipmentID from the subquery --
SELECT EquipmentID, Odometer, TripDate, (SELECT YourTable.Odometer -
[XX].Odometer FROM YourTable AS [XX] WHERE [XX].EquipmentID =
YourTable.EquipmentID AND Max([XX].Odometer) YourTable.Odometer ORDER BY
[XX].Odometer DEC) AS TripMileage
FROM YourTable
ORDER BY YourTable.Odometer;

--
Build a little, test a little.


"Bruce" wrote:

I am running Access 2007 SP2 on Windows XP Professional SP 3. I have a query
that I need to run a subquery to obtain a value in the previous record.
Basically I have a query that contains mileages grouped by EquipmentID and
month. I need to calculate miles driven in a given month. I need the last
mileage reading in the previous months record to run the calculation on the
month that has focus. I know that I have to alias the query, but that is
where my memory ends. I have done this before with help from this site and
hope to be as fortunate again.
--
Bruce

  #10  
Old March 19th, 2010, 11:36 PM posted to microsoft.public.access.queries
KARL DEWEY
external usenet poster
 
Posts: 10,767
Default SubQueries

Try this --
SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage, (SELECT
TOP 1 [Dupe].Mileage FROM [Monthly Miles from Master FD 03] AS [Dupe] WHERE
[Dupe].EquipmentID = [Monthly Miles from Master FD 03].EquipmentID AND
[Dupe].YearMonth [Monthly Miles from Master FD 03].YearMonth ORDER BY
[Dupe].[YearMonth] DESC) AS PreviousMileage
FROM [Monthly Miles from Master FD 03]
WHERE (([Monthly Miles from Master FD 03].EquipmentID)="UD793");

--
Build a little, test a little.


"Bruce" wrote:

John,

I got the query to run. However the query appears to be stuck in a loop. I
limited the selection to UD793, there should have been 26 records. There were
many interations of 26 records, I suspect there would have been at least 650
records. How do I code the query so it only runs once and does not keep
accessing the same records? Here is code that I used to get the query to run:
(I had to add the table and alias in the properties window)

SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage, (SELECT
TOP 1 [Dupe].Mileage FROM [Monthly Miles from Master FD 03] AS [Dupe] WHERE
[Dupe].EquipmentID = [Monthly Miles from Master FD 03].EquipmentID AND
[Dupe].YearMonth [Monthly Miles from Master FD 03].YearMonth ORDER BY
[Dupe].[YearMonth] DESC) AS PreviousMileage
FROM [Monthly Miles from Master FD 03], [Monthly Miles from Master FD 03] AS
Dupe
WHERE (([Monthly Miles from Master FD 03].EquipmentID)="UD793");
--
Bruce


"John Spencer" wrote:

I would try something along the following lines

To fix the syntax try removing the parenthesis Before the ORDER BY in the
sub-query and adding a closing parenthesis after DESC.

SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage
, (SELECT TOP 1 [Dupe].Mileage
FROM [Monthly Miles from Master FD 03] AS [Dupe]
WHERE [Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID]
AND [Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]
ORDER BY [Dupe].[YearMonth] DESC) AS PreviousMileage

FROM [Monthly Miles from Master FD 03];

That may still error. I would try something like the following.

SELECT [Monthly Miles from Master FD 03].EquipmentID
, [Monthly Miles from Master FD 03].YearMonth
, [Monthly Miles from Master FD 03].Mileage

, (SELECT Max([Dupe].Mileage)
FROM [Monthly Miles from Master FD 03] AS [Dupe]
WHERE [Dupe].[EquipmentID] =
[Monthly Miles from Master FD 03].[EquipmentID]
AND Dupe.Mileage [Monthly Miles from Master FD 03].[Mileage]) AS
PreviousMileage

FROM [Monthly Miles from Master FD 03];

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Bruce wrote:
Karl,

Thank you for your reply. I keep getting the following error message:

Syntax error (missing operator) in query expression ‘(Select Top 1
[Dupe].Mileage FROM [Monthly Miles Master FD 03] AS [Dupe] WHERE
[Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID] AND
[Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]) ORDER BY
[Dupe].[YearMonth’.

This is the entire string:

SELECT [Monthly Miles from Master FD 03].EquipmentID, [Monthly Miles from
Master FD 03].YearMonth, [Monthly Miles from Master FD 03].Mileage, (SELECT
TOP 1 [Dupe].Mileage FROM [Monthly Miles from Master FD 03] AS [Dupe] WHERE
[Dupe].[EquipmentID] = [Monthly Miles from Master FD 03].[EquipmentID] AND
[Dupe].[YearMonth] [Monthly Miles from Master FD 03].[YearMonth]) ORDER BY
[Dupe].[YearMonth] DESC AS PreviousMileage
FROM [Monthly Miles from Master FD 03];

I have tried several different configurations to no avail; can you tell me
what I am doing wrong??

.

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 07:55 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.