×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

SQL Aggregate question

SQL Aggregate question

SQL Aggregate question

(OP)
I am using max() function and want to know 2 values associated with the max. This is my query
BE
=====
Tnum    BE_id    BET
---------------------
1        1       45
1        2        3
1        3        6
1        4       54
1        5        1

I want to get Max(BE_id) and BET and Tnum associated with it.

Result
================
Tnum    BE_id    BET
---------------------
1        5        1

Can i have an SQL for this without having to join the table with itself.

Any help is greatly appreciated..

Thanks in advance

RE: SQL Aggregate question

You'll usually get better answers posting in the specific forum for the database you're working with rather than the General Database Discussion forum, but this should work for you:

SELECT TNum, MaxBE_ID, BET FROM TableName A
INNER JOIN (SELECT TNum, Max(BE_ID) As MaxBE_ID FROM TableName GROUP BY TNum) B ON A.TNum = B.Tnum AND A.BE_ID = B.MaxBE_ID

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual

Essential reading for anyone working with databases:
The Fundamentals of Relational Database Design
Understanding SQL Joins

RE: SQL Aggregate question

CODE

SELECT Tnum, BE_id, BET
FROM tablename
WHERE BE_id = (SELECT MAX(BE_id) FROM tablename)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close