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 485801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:26:18+00:00 2026-05-13T01:26:18+00:00

I have a table relationship that looks like the following: barn —— PK barn_id

  • 0

I have a table relationship that looks like the following:

barn
------
PK barn_id
<other columns>

stable
---------
PK stable_id
FK barn_id
stable_number
stable_contents
timestamp

So whenever the contents of a stable change I just put in a new row with the corresponding barn_id and stable_number with new stable_contents and a current timestamp.

The tables are designed this way so I can look at a certain stable and see its entire history.

I am trying to write a query that will find me the current state of all the stables in all the barns, so I try this:

SELECT barn_id, stable_number, max(timestamp), stable_contents
FROM stable
GROUP BY barn_id, stable_number

In my test data I have some rows like this for barn 1, stable 7

1 | 7 | 2009-12-09 10:00:00 | empty
1 | 7 | 2009-12-10 10:30:00 | show horse

If I run the SELECT query above, I get the following row returned for barn 1, stable 7:

1 | 7 | 2009-12-10 10:30:00 | empty

it gets the right maximum timestamp, just the wrong stable_contents.

Any ideas?

  • 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-05-13T01:26:19+00:00Added an answer on May 13, 2026 at 1:26 am

    It really should give you an error instead of returning undefined data because you’re trying to get non-aggregated data that isn’t in your GROUP BY (stable_contents). I would use the following query, which finds all rows for a stable where there isn’t a row after it for the same stable:

    SELECT
         T1.barn_id,
         T1.stable_number,
         T1.timestamp,
         T1.stable_contents
    FROM
         Stable AS T1
    LEFT OUTER JOIN Stable AS T2 ON
         T2.barn_id = T1.barn_id AND
         T2.stable_number = T1.stable_number AND
         T2.timestamp > T1.timestamp
    WHERE
         T2.barn_id IS NULL     -- The only way for this to be NULL is if no match was found
    

    Alternatively:

    SELECT
         T1.barn_id,
         T1.stable_number,
         T1.timestamp,
         T1.stable_contents
    FROM
         Stable AS T1
    WHERE
         NOT EXISTS
         (
              SELECT
                   *
              FROM
                   Stable AS T2
              WHERE
                   T2.barn_id = T1.barn_id AND
                   T2.stable_number = T1.stable_number AND
                   T2.timestamp > T1.timestamp
         )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a categories table that looks like this: id | name | parent
I have a BUSINESS table that looks like this: BUSINESS_ID | BRN | CODE
i have a many to many table relationship that involves 2 logical tables. Record
I have an table Article that is in relationship with Steps (OneToMany) and Steps
I have a table that shows the relationships between people a bit like this.
I want to have a database table that keeps data with revision history (like
I have an abstract entity that 4 other entities inherit from. This relationship works
I have schema that looks like this: create_table users, :force => true do |t|
I have two tables that should be joined together by a foreign key relationship,
I have 2 NHibernate entities (tables) that are in a one to many relationship.

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.