473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Need SQL code for fields when using MAX function

When you use an aggregate function in a SELECT statement, you cannot specify
any fields you want they way you usually can in a SELECT statement. Only
fields that are part of the GROUP BY clause can be retrieved. How can I
create a join to select a record from a table using the MAX function and
then have use of all of the fields in that record? Anyone know?

This is something I have tried but it does not seem to work in Access:

SELECT t1.Column1, t1.Column2, t2.Column3, t2.Column4
FROM
(
SELECT Column1, MAX(Column2) AS Column2
FROM Table1
GROUP BY Column1

) t1
INNER JOIN Table1 t2
ON t1.Column1 = t2.Column1 AND t1.Column2 = t2.Column2
Thanks.

Robert

Nov 13 '05 #1
2 5101
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Perhaps this:

SELECT col1, col2, col3, col4
FROM table_name t1
WHERE col2 = (SELECT max(col2) FROM table_name
WHERE col1 = t1.col1)

Access JET SQL likes square brackets around tabular subqueries. E.g.:

SELECT ...
FROM [SELECT ... ]. As t1 INNER JOIN t2 ON t1.col = t2.col
WHERE ...

Note the period immediately succeeding the right square-bracket.

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQdTV/4echKqOuFEgEQJ2ewCfbAVuDkQE3Q1qN+0WG7ioIEEND+0Anjp q
8K9mSHDBFnIjiFCMmB+dDYka
=Xbal
-----END PGP SIGNATURE-----
Robert wrote:
When you use an aggregate function in a SELECT statement, you cannot specify
any fields you want they way you usually can in a SELECT statement. Only
fields that are part of the GROUP BY clause can be retrieved. How can I
create a join to select a record from a table using the MAX function and
then have use of all of the fields in that record? Anyone know?

This is something I have tried but it does not seem to work in Access:

SELECT t1.Column1, t1.Column2, t2.Column3, t2.Column4
FROM
(
SELECT Column1, MAX(Column2) AS Column2
FROM Table1
GROUP BY Column1

) t1
INNER JOIN Table1 t2
ON t1.Column1 = t2.Column1 AND t1.Column2 = t2.Column2

Nov 13 '05 #2
I think this is working. You could also use SELECT TOP 1 ... ORDER BY col2
DESC, I think. Not sure which is better.

Thanks.

Robert

"MGFoster" <me@privacy.com> wrote in message
news:gC***************@newsread3.news.pas.earthlin k.net...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Perhaps this:

SELECT col1, col2, col3, col4
FROM table_name t1
WHERE col2 = (SELECT max(col2) FROM table_name
WHERE col1 = t1.col1)

Access JET SQL likes square brackets around tabular subqueries. E.g.:

SELECT ...
FROM [SELECT ... ]. As t1 INNER JOIN t2 ON t1.col = t2.col
WHERE ...

Note the period immediately succeeding the right square-bracket.

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQdTV/4echKqOuFEgEQJ2ewCfbAVuDkQE3Q1qN+0WG7ioIEEND+0Anjp q
8K9mSHDBFnIjiFCMmB+dDYka
=Xbal
-----END PGP SIGNATURE-----
Robert wrote:
When you use an aggregate function in a SELECT statement, you cannot
specify any fields you want they way you usually can in a SELECT
statement. Only fields that are part of the GROUP BY clause can be
retrieved. How can I create a join to select a record from a table using
the MAX function and then have use of all of the fields in that record?
Anyone know?

This is something I have tried but it does not seem to work in Access:

SELECT t1.Column1, t1.Column2, t2.Column3, t2.Column4
FROM
(
SELECT Column1, MAX(Column2) AS Column2
FROM Table1
GROUP BY Column1

) t1
INNER JOIN Table1 t2
ON t1.Column1 = t2.Column1 AND t1.Column2 = t2.Column2

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike Button | last post by:
Hello all, I am really really desperate on what I should do, and I am asking for help from anyone in this newsgroup, here's the situation: I am creating a form that is being run on a server...
1
by: LenS | last post by:
If this is the wrong place to post this, please advise better place. Otherwise, I have created the following python program and it works. Running on XP. I think I am now at that stage of learning...
8
by: Bill | last post by:
Hello out there; This may be a challenge but I'm certain it's possible but I can't seem to figure out how. I have a table that has several date fields, e.g., Date1, Date2, Date3, Date4 ......
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
11
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
3
by: Martin | last post by:
Hi all, I have some VBScript code that I am trying to port over to VB.NET. I am receiving an error "Cast from type 'Field' to type 'String' is not valid." when calling the MapLegacyExchangeDN...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
4
by: pbd22 | last post by:
Hi. In my script the below code creates a new element on the page with an associated delete button: var row_element = new Element( 'div', { 'class':'container', 'events':{
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.