SQL Query MAX Date

psulions83

Board Regular
Joined
Nov 16, 2015
Messages
126
Hi,

I have the following query that runs but gives me too many lines.

I only want it to return the same amount of lines that exist on table C and give me the value for A.AMOUNT with the latest date which is in a column called DATE.

Code:
SELECT      B.BRAND,
      C.CONTRACT,
      C.CUSTOMER,
      A.AMOUNT
   FROM Tbl_Price A,
               Tbl_Data B,
               Tbl_12mth AS C
      WHERE A.PRODUCT = B.PRODUCT
           AND B.PRODUCT = C.PRODUCT
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Can you please post sample data from all three tables (we really only need to see all the fields used in the Query for each Table and the Date field)?
 
Upvote 0
Agreed. However, I see no joins between tables, so you are probably getting LOTS of results. This would be known as a Cartesian product. If each table has 25 records, you would get 25x25x25 records if there was no criteria. That you are filtering on = fields means who knows how many in the results.
 
Upvote 0
> I see no joins between tables,

they're doing old style joins

WHERE A.PRODUCT = B.PRODUCT
AND B.PRODUCT = C.PRODUCT

don't know if it still works in current version of access

 
Upvote 0
I don't see anything in the query that references a max date so that may be part of the problem. The older join syntax does still work even if not so commonly seen these days.

This *might* work (unfortunately subqueries are not where MSAccess shines):
Code:
SELECT      
	B.BRAND,
	C.CONTRACT,
	C.CUSTOMER,
	A.AMOUNT
FROM
	Tbl_Price A 
	INNER JOIN Tbl_Data B
	ON A.PRODUCT = B.PRODUCT
	INNER JOIN Tbl_12mth C
	ON B.PRODUCT = C.PRODUCT
WHERE 
	A.[DATE] = (SELECT MAX(A2.[DATE]) FROM Tbl_Price A2 WHERE A2.PRODUCT = A.PRODUCT)

Agree that some sample data would help for testing and clarifying the data relationships.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,574
Messages
6,120,327
Members
448,956
Latest member
Adamsxl

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