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  

how to get last records out of 4 records per customer?



 
 
Thread Tools Display Modes
  #1  
Old December 22nd, 2009, 04:09 PM posted to microsoft.public.access.queries
mls via AccessMonster.com
external usenet poster
 
Posts: 46
Default how to get last records out of 4 records per customer?

I am having 4 records for each customer with different dates, so how can I
get the last records for this customer. I also want to see 4 other fields in
my results along with the customer no and date.
I tried to use last in my select query for customer, but what should I give
to other fields because I don't wan tot perform any summary function on them.

Thanks.

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...eries/200912/1

  #2  
Old December 22nd, 2009, 04:21 PM posted to microsoft.public.access.queries
mls via AccessMonster.com
external usenet poster
 
Posts: 46
Default how to get last records out of 4 records per customer?

Infact I want to get all the details of a customer for his latest order
number. so for example a customer has 4 orders then I want to see all his
details for order_num4 irrespective of dates. How can I get them?

mls wrote:
I am having 4 records for each customer with different dates, so how can I
get the last records for this customer. I also want to see 4 other fields in
my results along with the customer no and date.
I tried to use last in my select query for customer, but what should I give
to other fields because I don't wan tot perform any summary function on them.

Thanks.


--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...eries/200912/1

  #3  
Old December 22nd, 2009, 04:22 PM posted to microsoft.public.access.queries
Allen Browne
external usenet poster
 
Posts: 11,706
Default how to get last records out of 4 records per customer?

Time to learn about subqueries.
http://allenbrowne.com/subquery-01.html

This kind of thing:

SELECT Table1.*
FROM Table1
WHERE ID =
(SELECT TOP 1 ID
FROM Table1 AS Dupe
WHERE Dupe.CustomerID = Table1.CustomerID
ORDER BY TheDate DESC, ID DESC);

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.


"mls via AccessMonster.com" u55943@uwe wrote in message
news:a0f9b452946fa@uwe...
I am having 4 records for each customer with different dates, so how can
I get the last records for this customer. I also want to see 4 other
fields
in my results along with the customer no and date.
I tried to use last in my select query for customer, but what should I
give
to other fields because I don't wan tot perform any summary function on
them.


  #4  
Old December 22nd, 2009, 05:21 PM posted to microsoft.public.access.queries
mls via AccessMonster.com
external usenet poster
 
Posts: 46
Default how to get last records out of 4 records per customer?

Some how my query is failing at WHERE Dupe.CustomerID = Table1.CustomerID
it says : "Invalid use of '.','!' or '()' in expression. but I don't see any
of those characters.
I run this different ways and every time it is failing there only


Allen Browne wrote:
Time to learn about subqueries.
http://allenbrowne.com/subquery-01.html

This kind of thing:

SELECT Table1.*
FROM Table1
WHERE ID =
(SELECT TOP 1 ID
FROM Table1 AS Dupe
WHERE Dupe.CustomerID = Table1.CustomerID
ORDER BY TheDate DESC, ID DESC);

I am having 4 records for each customer with different dates, so how can
I get the last records for this customer. I also want to see 4 other

[quoted text clipped - 4 lines]
to other fields because I don't wan tot perform any summary function on
them.


--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...eries/200912/1

  #5  
Old December 22nd, 2009, 06:13 PM posted to microsoft.public.access.queries
KARL DEWEY
external usenet poster
 
Posts: 10,767
Default how to get last records out of 4 records per customer?

Copy and paste the SQL from your query for analyses.

--
Build a little, test a little.


"mls via AccessMonster.com" wrote:

Some how my query is failing at WHERE Dupe.CustomerID = Table1.CustomerID
it says : "Invalid use of '.','!' or '()' in expression. but I don't see any
of those characters.
I run this different ways and every time it is failing there only


Allen Browne wrote:
Time to learn about subqueries.
http://allenbrowne.com/subquery-01.html

This kind of thing:

SELECT Table1.*
FROM Table1
WHERE ID =
(SELECT TOP 1 ID
FROM Table1 AS Dupe
WHERE Dupe.CustomerID = Table1.CustomerID
ORDER BY TheDate DESC, ID DESC);

I am having 4 records for each customer with different dates, so how can
I get the last records for this customer. I also want to see 4 other

[quoted text clipped - 4 lines]
to other fields because I don't wan tot perform any summary function on
them.


--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...eries/200912/1

.

  #6  
Old December 22nd, 2009, 06:27 PM posted to microsoft.public.access.queries
mls via AccessMonster.com
external usenet poster
 
Posts: 46
Default how to get last records out of 4 records per customer?

I am trying to get the latest Test results for each ID.

SELECT SPECIMEN.ID, SPECIMEN.TestNo, SPECIMEN.Run_Date, SPECIMEN.
date_resulted
FROM SPECIMEN
WHERE SPECIMEN.ID IN (SELECT TOP 1 TestNo FROM SPECIMEN AS Dupe
WHERE Dupe.ID = SPECIMEN. ID
ORDER BY SPECIMEN.TestNo DESC);




KARL DEWEY wrote:
Copy and paste the SQL from your query for analyses.

Some how my query is failing at WHERE Dupe.CustomerID = Table1.CustomerID
it says : "Invalid use of '.','!' or '()' in expression. but I don't see any

[quoted text clipped - 19 lines]
to other fields because I don't wan tot perform any summary function on
them.


--
Message posted via http://www.accessmonster.com

  #7  
Old December 22nd, 2009, 06:44 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default how to get last records out of 4 records per customer?

Take a look at your WHERE clause. You are attempting to match Id to TestNo.

SELECT SPECIMEN.ID, SPECIMEN.TestNo
, SPECIMEN.Run_Date
, SPECIMEN.date_resulted
FROM SPECIMEN
WHERE SPECIMEN.TestNo IN (SELECT TOP 1 TestNo FROM SPECIMEN AS Dupe
WHERE Dupe.ID = SPECIMEN. ID
ORDER BY SPECIMEN.TestNo DESC);

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

mls via AccessMonster.com wrote:
I am trying to get the latest Test results for each ID.

SELECT SPECIMEN.ID, SPECIMEN.TestNo, SPECIMEN.Run_Date, SPECIMEN.
date_resulted
FROM SPECIMEN
WHERE SPECIMEN.ID IN (SELECT TOP 1 TestNo FROM SPECIMEN AS Dupe
WHERE Dupe.ID = SPECIMEN. ID
ORDER BY SPECIMEN.TestNo DESC);




KARL DEWEY wrote:
Copy and paste the SQL from your query for analyses.

Some how my query is failing at WHERE Dupe.CustomerID = Table1.CustomerID
it says : "Invalid use of '.','!' or '()' in expression. but I don't see any

[quoted text clipped - 19 lines]
to other fields because I don't wan tot perform any summary function on
them.


  #8  
Old December 22nd, 2009, 06:56 PM posted to microsoft.public.access.queries
mls via AccessMonster.com
external usenet poster
 
Posts: 46
Default how to get last records out of 4 records per customer?

Even if I give ID it is not working, showing the same error

SELECT SPECIMEN.ID, SPECIMEN.TestNo
, SPECIMEN.Run_Date
, SPECIMEN.date_resulted
FROM SPECIMEN
WHERE SPECIMEN.TestNo IN (SELECT TOP 1 ID FROM SPECIMEN AS Dupe
WHERE Dupe.ID = SPECIMEN. ID
ORDER BY SPECIMEN.TestNo DESC);



John Spencer wrote:
Take a look at your WHERE clause. You are attempting to match Id to TestNo.

SELECT SPECIMEN.ID, SPECIMEN.TestNo
, SPECIMEN.Run_Date
, SPECIMEN.date_resulted
FROM SPECIMEN
WHERE SPECIMEN.TestNo IN (SELECT TOP 1 TestNo FROM SPECIMEN AS Dupe
WHERE Dupe.ID = SPECIMEN. ID
ORDER BY SPECIMEN.TestNo DESC);

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

I am trying to get the latest Test results for each ID.

[quoted text clipped - 12 lines]
to other fields because I don't wan tot perform any summary function on
them.


--
Message posted via http://www.accessmonster.com

  #9  
Old December 22nd, 2009, 07:17 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default how to get last records out of 4 records per customer?

Also, I missed a SPACE between Specimen. and ID in the sub-query.

SELECT SPECIMEN.ID, SPECIMEN.TestNo
, SPECIMEN.Run_Date
, SPECIMEN.date_resulted
FROM SPECIMEN
WHERE SPECIMEN.TestNo IN (SELECT TOP 1 TestNo FROM SPECIMEN AS Dupe
WHERE Dupe.ID = SPECIMEN.ID
ORDER BY SPECIMEN.TestNo DESC);

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

John Spencer wrote:
Take a look at your WHERE clause. You are attempting to match Id to
TestNo.

SELECT SPECIMEN.ID, SPECIMEN.TestNo
, SPECIMEN.Run_Date
, SPECIMEN.date_resulted
FROM SPECIMEN
WHERE SPECIMEN.TestNo IN (SELECT TOP 1 TestNo FROM SPECIMEN AS Dupe
WHERE Dupe.ID = SPECIMEN. ID
ORDER BY SPECIMEN.TestNo DESC);

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

mls via AccessMonster.com wrote:
I am trying to get the latest Test results for each ID.

SELECT SPECIMEN.ID, SPECIMEN.TestNo, SPECIMEN.Run_Date, SPECIMEN.
date_resulted FROM SPECIMEN
WHERE SPECIMEN.ID IN (SELECT TOP 1 TestNo FROM SPECIMEN AS Dupe
WHERE Dupe.ID = SPECIMEN. ID ORDER BY SPECIMEN.TestNo DESC);




KARL DEWEY wrote:
Copy and paste the SQL from your query for analyses.

Some how my query is failing at WHERE Dupe.CustomerID =
Table1.CustomerID it says : "Invalid use of '.','!' or '()' in
expression. but I don't see any
[quoted text clipped - 19 lines]
to other fields because I don't wan tot perform any summary
function on them.


  #10  
Old December 22nd, 2009, 07:44 PM posted to microsoft.public.access.queries
mls via AccessMonster.com
external usenet poster
 
Posts: 46
Default how to get last records out of 4 records per customer?

After removing the space that error is gone now.
But the query is resulting all the rows not just the top 1.


John Spencer wrote:
Also, I missed a SPACE between Specimen. and ID in the sub-query.

SELECT SPECIMEN.ID, SPECIMEN.TestNo
, SPECIMEN.Run_Date
, SPECIMEN.date_resulted
FROM SPECIMEN
WHERE SPECIMEN.TestNo IN (SELECT TOP 1 TestNo FROM SPECIMEN AS Dupe
WHERE Dupe.ID = SPECIMEN.ID
ORDER BY SPECIMEN.TestNo DESC);

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

Take a look at your WHERE clause. You are attempting to match Id to
TestNo.

[quoted text clipped - 27 lines]
to other fields because I don't wan tot perform any summary
function on them.


--
Message posted via http://www.accessmonster.com

 




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 01:57 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.