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  

Select Max, return unwanted rows.



 
 
Thread Tools Display Modes
  #1  
Old April 26th, 2010, 07:35 AM posted to microsoft.public.access.queries
Aldred@office
external usenet poster
 
Posts: 28
Default Select Max, return unwanted rows.

Hi all,
I have searched this forum with keyword max, but those solutions doesn't
seem to work on my problem. Can some one please help in take a look?

I have 3 tables look like this:

tClient
ID, other fields...

tPart
ID, ClientID, PartNum, other fields...

tCharge
ID, partNumID, Charge, ChargeDate(This is a Date/time field), other
fields...

I have a query looks like this:

Select PartNumID, Charge, max(ChargeDate)
from tClient inner Join (tPart inner Join tCharge on tPart.ID =
tCharge.PartNumID) on tClient.ID = tPart.ClientID
Where ClientID = 9
Group by PartNumID, Charge

It returns something liks this
PartNumID Charge Expr1002
382 HK$0.33 26/5/2009 13:30:30
382 HK$0.38 1/4/2010 11:16:56
383 HK$0.39 26/5/2009 13:31:11
383 HK$0.45 1/4/2010 11:31:17
..
..
..

But obviously, I'd like to have the query return something like this:
PartNumID Charge Expr1002
382 HK$0.38 1/4/2010 11:16:56
383 HK$0.45 1/4/2010 11:31:17

I tried something like this and it returns nothing:
Select PartNumID, Charge, ChargeDate from tClient inner Join (tPart inner
Join tCharge on tPart.ID = tCharge.PartNumID) on tClient.ID = tPart.ClientID
Where ChargeDate = (Select Max(ChargeDate) from tPart inner Join tCharge on
tPart.ID = tCharge.PartNumID) and ClientID = 9
Order by PartNUmID

Looks like the query above will return only the max date from the table
tcharge and it is not linked with ClientID 9 so it returns nothing.

Can some one please help in pointing me what's wrong?

Thank you so much in advance.

  #2  
Old April 26th, 2010, 10:09 AM posted to microsoft.public.access.queries
Stefan Hoffmann
external usenet poster
 
Posts: 991
Default Select Max, return unwanted rows.

hi Aldred,

On 26.04.2010 08:35, Aldred@office wrote:
Can some one please help in pointing me what's wrong?

Try this:

SELECT *
FROM tPart oP
INNER JOIN tCharge oC
ON oC.partNumID = P.ID
WHERE oC.ChargeDate =
(
SELECT Max(iC.ChargeDate)
FROM tCharge iC
INNER JOIN tPart iP
ON iP.ID = iC.partNumID
WHERE iP.ID = oP.ID
)




mfG
-- stefan --
  #3  
Old April 26th, 2010, 09:01 PM posted to microsoft.public.access.queries
KARL DEWEY
external usenet poster
 
Posts: 10,767
Default Select Max, return unwanted rows.

UNTESTED UNTESTED
Try this --
Select PartNumID, Charge, ChargeDate
from tClient inner Join (tPart inner Join tCharge on tPart.ID =
tCharge.PartNumID) on tClient.ID = tPart.ClientID
Where ClientID = 9 AND DateValue(tCharge.ChargeDate) = (SELECT
Max(DateValue([XX].ChargeDate)) FROM tCharge AS [XX] WHERE [XX].PartNumID =
tPart.ID)
ORDER BY PartNumID, Charge, ChargeDate;

--
Build a little, test a little.


"Aldred@office" wrote:

Hi all,
I have searched this forum with keyword max, but those solutions doesn't
seem to work on my problem. Can some one please help in take a look?

I have 3 tables look like this:

tClient
ID, other fields...

tPart
ID, ClientID, PartNum, other fields...

tCharge
ID, partNumID, Charge, ChargeDate(This is a Date/time field), other
fields...

I have a query looks like this:

Select PartNumID, Charge, max(ChargeDate)
from tClient inner Join (tPart inner Join tCharge on tPart.ID =
tCharge.PartNumID) on tClient.ID = tPart.ClientID
Where ClientID = 9
Group by PartNumID, Charge

It returns something liks this
PartNumID Charge Expr1002
382 HK$0.33 26/5/2009 13:30:30
382 HK$0.38 1/4/2010 11:16:56
383 HK$0.39 26/5/2009 13:31:11
383 HK$0.45 1/4/2010 11:31:17
.
.
.

But obviously, I'd like to have the query return something like this:
PartNumID Charge Expr1002
382 HK$0.38 1/4/2010 11:16:56
383 HK$0.45 1/4/2010 11:31:17

I tried something like this and it returns nothing:
Select PartNumID, Charge, ChargeDate from tClient inner Join (tPart inner
Join tCharge on tPart.ID = tCharge.PartNumID) on tClient.ID = tPart.ClientID
Where ChargeDate = (Select Max(ChargeDate) from tPart inner Join tCharge on
tPart.ID = tCharge.PartNumID) and ClientID = 9
Order by PartNUmID

Looks like the query above will return only the max date from the table
tcharge and it is not linked with ClientID 9 so it returns nothing.

Can some one please help in pointing me what's wrong?

Thank you so much in advance.

  #4  
Old April 28th, 2010, 06:37 AM posted to microsoft.public.access.queries
Aldred@office
external usenet poster
 
Posts: 28
Default ¦^ÂÐ: Select Max, return unwanted rows.

Hi Stefan,
Access complained that syntax error in join. Will that be the problem of
having no "AS"?

Let me try to put "AS" in and see if it works.

Thanks.

"Stefan Hoffmann" ¦b¶l¥ó
¤¤¼¶¼g...
hi Aldred,

On 26.04.2010 08:35, Aldred@office wrote:
Can some one please help in pointing me what's wrong?

Try this:

SELECT *
FROM tPart oP
INNER JOIN tCharge oC
ON oC.partNumID = P.ID
WHERE oC.ChargeDate =
(
SELECT Max(iC.ChargeDate)
FROM tCharge iC
INNER JOIN tPart iP
ON iP.ID = iC.partNumID
WHERE iP.ID = oP.ID
)




mfG
-- stefan --


  #5  
Old April 28th, 2010, 06:37 AM posted to microsoft.public.access.queries
Aldred@office
external usenet poster
 
Posts: 28
Default 回覆: Select Max, return unwanted rows.

Thanks. It works just what I want.

I will need to study why this query works but mine doesn't.

"KARL DEWEY" 在郵件
ä¸*撰寫...
UNTESTED UNTESTED
Try this --
Select PartNumID, Charge, ChargeDate
from tClient inner Join (tPart inner Join tCharge on tPart.ID =
tCharge.PartNumID) on tClient.ID = tPart.ClientID
Where ClientID = 9 AND DateValue(tCharge.ChargeDate) = (SELECT
Max(DateValue([XX].ChargeDate)) FROM tCharge AS [XX] WHERE [XX].PartNumID
=
tPart.ID)
ORDER BY PartNumID, Charge, ChargeDate;

--
Build a little, test a little.


"Aldred@office" wrote:

Hi all,
I have searched this forum with keyword max, but those solutions doesn't
seem to work on my problem. Can some one please help in take a look?

I have 3 tables look like this:

tClient
ID, other fields...

tPart
ID, ClientID, PartNum, other fields...

tCharge
ID, partNumID, Charge, ChargeDate(This is a Date/time field), other
fields...

I have a query looks like this:

Select PartNumID, Charge, max(ChargeDate)
from tClient inner Join (tPart inner Join tCharge on tPart.ID =
tCharge.PartNumID) on tClient.ID = tPart.ClientID
Where ClientID = 9
Group by PartNumID, Charge

It returns something liks this
PartNumID Charge Expr1002
382 HK$0.33 26/5/2009 13:30:30
382 HK$0.38 1/4/2010 11:16:56
383 HK$0.39 26/5/2009 13:31:11
383 HK$0.45 1/4/2010 11:31:17
.
.
.

But obviously, I'd like to have the query return something like this:
PartNumID Charge Expr1002
382 HK$0.38 1/4/2010 11:16:56
383 HK$0.45 1/4/2010 11:31:17

I tried something like this and it returns nothing:
Select PartNumID, Charge, ChargeDate from tClient inner Join (tPart inner
Join tCharge on tPart.ID = tCharge.PartNumID) on tClient.ID =
tPart.ClientID
Where ChargeDate = (Select Max(ChargeDate) from tPart inner Join tCharge
on
tPart.ID = tCharge.PartNumID) and ClientID = 9
Order by PartNUmID

Looks like the query above will return only the max date from the table
tcharge and it is not linked with ClientID 9 so it returns nothing.

Can some one please help in pointing me what's wrong?

Thank you so much in advance.


 




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 02:05 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.