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

  • Home
  • SEARCH
  • 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 8884553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:05:17+00:00 2026-06-14T21:05:17+00:00

I have a table called Objects which contains some files, say: User Teacher There

  • 0

I have a table called Objects which contains some files, say:

  1. User
  2. Teacher

There is another table (States) which holds the possible states of these objects, like:

  1. Active
  2. Idle
  3. Teaching
  4. Resting
  5. Authoring

And there is a third table (junction table) which logs each state change of each object. In this third table (ObjectStates) records are like:

  1. 1, 1, DateTime1 (User was active on DateTime1)
  2. 2, 5, DateTime2 (Teacher was authoring on DateTime2)

etc.

Now, what I want is a query to get each object, with its latest state (not state history). It’s possible to get this result using cursors, or using Cross Apply command. However, I’d like to know if there is any other way to get the latest states of each object from these three tables? Because cursors are costy.

  • 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-14T21:05:18+00:00Added an answer on June 14, 2026 at 9:05 pm

    Using the row_number() windowing function…

    select * 
    from
    (
    
    select objects.*,
           state.state,
           objectstates.changedate,
           row_number() over (partition by object.objectid order by changedate desc) rn
    from 
        objects
             inner join
        objectstates
             on objects.id = objectstates.objectid
             inner join
        states 
             on objectstates.stateid = states.stateid
    ) v
    where rn = 1
    

    If you can’t use row_number because you’re on SQL 2000, for example, you can use a join on a max/group by query.

    select objects.*,
           state.state,
           objectstates.changedate,
    from 
        objects
             inner join
        objectstates
             on objects.id = objectstates.objectid
             inner join
        states 
             on objectstates.stateid = states.stateid
        inner join
             (select objectid, max(changedate) as maxdate from objectstates group by objectid) maxstates
             on objectstates.objectid=maxstates.objectid
             and objectstates.changedate = maxstates.maxdate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have table called as Support, which have a field named Name and contains
I'm using a dataset. I have a table Adapter called PackageTableAdapter , which contains
I have a table called Products which obviously contains products. However, I need to
I have a table called users which currently contains column money of type integer
I have this table which contains a field called 'icon_for' which I have been
I have Master1 and Sub1 and a another called Master1Sub1_Map which contains foreign keys
I have a table in LINQ to SQL called Cars that contains Car objects.
I have a class called Contacts which contains a collection of objects called PhoneNumbers
I have a table called States and i am displaying values of states in
I have a table called TPM_TASKS which contain all tasks, as well as a

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.