Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8623737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:21:21+00:00 2026-06-12T07:21:21+00:00

I have an Address History table with three fields: EmpID , Address , AddrID

  • 0

I have an Address History table with three fields: EmpID, Address, AddrID.

Every time I add a new address, I also increment the Address ID (AddrID) by 1 for that particular employee.

EmpID | AddrID | Address
-------------------------------
 1    |     1  | 1234 First Ave
 1    |     2  | 2145 First Ave
 1    |     3  | 1111 First Ave

 2    |     1  | 1001 Second St
 2    |     2  | 1002 Second St
 2    |     3  | 1003 Second St
 2    |     4  | 2222 Second St

 3    |     1  | 3332 Third Lane
 3    |     2  | 3333 Third Lane

 4    |     1  | 4444 Fourth Way

How do I get the most recent address (highest Address ID) for each employee? Ideally, I should be able to return:

EmpID | AddrID | Address
-------------------------------
 1    |     3  | 1111 First Ave
 2    |     4  | 2222 Second St
 3    |     2  | 3333 Third Lane
 4    |     1  | 4444 Fourth Way

So far I have either returned too many results (ie, every Employee, every AddrID 1, and every Address associated with the two), or too few results (ie, every Employee with an AddrID 4 – just Employee 2).

I have tried using Distinct, Group By, Order By, Having, and Self-Joins to no avail.

What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:21:22+00:00Added an answer on June 12, 2026 at 7:21 am

    You can use a subquery that gets the MAX() addrid for each empid:

    select t1.empid,
      t1.addrid,
      t1.address
    from table1 t1
    inner join
    (
      select max(addrid) addrid, empid
      from table1
      group by empid
    ) t2
      on t1.empid = t2.empid
      and t1.addrid = t2.addrid
    

    See SQL Fiddle With Demo

    The inner query will return the max addrid and the empid then you join your table to that result on those two values this will limit the records that get returned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that records a history of address updates, called transaction. The
I have address table in which city field have nvarchar datatype, but I want
I am new to iphone development. In my application, I have address for a
Currently I have an address column that is an embedded document in my users
I have a database with Points of Interest that all have an address. I
Say that you have a table with two (or more) columns which link to
I have a table with text box asking for Name and Address for reference.
I have a website that uses the jQuery Address plugin from Asual for navigation.
I have a address like line = 12345 North Drive,,Palm Retreat, CO,CO,92261 When I
I have an address database and I tried to find latitude and longitude using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.