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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:10:11+00:00 2026-05-26T23:10:11+00:00

There are 2 tables involved in this question: Table1: |- Time Stamp -|- Special

  • 0

There are 2 tables involved in this question:

Table1:

|- Time Stamp -|- Special Number -|- Other Data -|
|- 2011       -|- 1              -|- green      -|
|- 2010       -|- 1              -|- blue       -|
|- 2009       -|- 2              -|- yellow     -|
|- 2011       -|- 3              -|- red        -|
|- 2010       -|- 3              -|- orange     -|
|- 2009       -|- 4              -|- purple     -|

Table 2:

|- Special Number -|- State (location) -|
|- 1              -|- Hawaii           -|
|- 2              -|- Hawaii           -|
|- 3              -|- Alaska           -|
|- 4              -|- Alaska           -|

Table 2 relates the ‘Special Number’ to a ‘State’

Now then, the result I am trying to get would look something like:

|- Time Stamp -|- State (location) -|- Other Data -|
|- 2011       -|- Hawaii           -|- green      -|
|- 2011       -|- Alaska           -|- red        -|

Where I am trying to get the MAX time stamp, grouped by each state, as well as the other data corresponding to that ‘latest time stamp’ row from Table 1.

If I do:

SELECT MAX(time stamp), state
FROM table 1, table 2
WHERE table 1.special number = table 2.special number
GROUP BY state

This returns the max time stamp, for each state (which is almost what I am looking for), but when I try to include ‘Other Data’, it returns all the records (since every ‘other data’ record is unique).

I hope someone can provide some ideas,
Thanks

EDIT:

Table 1 has a Unique ID column:

|- Time Stamp -|- Special Number -|- Other Data -|- Unique Row ID -|
|- 2011       -|- 1              -|- green      -| 0              -|
|- 2010       -|- 1              -|- blue       -| 1              -|
|- 2009       -|- 2              -|- yellow     -| 2              -|
|- 2011       -|- 3              -|- red        -| 3              -|
|- 2010       -|- 3              -|- orange     -| 4              -|
|- 2009       -|- 4              -|- purple     -| 5              -|

EDIT 2: SOLUTION * * * * Thanks to everyone who posted ! * * * *

 SELECT t1.timestamp, t2.specialNumber, t1.otherData
 FROM Table1 t1 inner join Table2 t2 on t1.specialNumber = t2.specialNumber
     inner join (select MAX(Table1.timestamp) maxts, Table2.state
         from Table1 inner join Table2 on Table1.specialNumber = Table2.specialNumber
         group by Table2.state) t3
     on t2.state = t3.state and t1.timestamp = t3.maxts

* whew *

  • 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-26T23:10:12+00:00Added an answer on May 26, 2026 at 11:10 pm

    Ahhh, the good old greatest-n-per-group…

    Here’s one way to do it:

    SELECT t1.TimeStamp, t2.State, t1.OtherData
    FROM Table1 t1
    inner join Table2 t2 
        on t1.SpecialNumber = t2.SpecialNumber
    inner join (SELECT MAX(time stamp) maxts, state
                FROM table1 inner join table2
                ON table1.specialnumber = table2.specialnumber
                GROUP BY state) t3
        on t2.State = t3.State and t1.TimeStamp = t3.maxts
    

    There’s a very comprehensive answer here: SQL Select only rows with Max Value on a Column

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

Sidebar

Related Questions

This question is about what happens with the reorganizing of data in a clustered
I'm asking this question because I'm getting a deadlock from time to time that
There are three tables, Students, Classes, Student_Class Is it possible to select students who
There are three tables in my database: apples, refrigerators, and houses. Each apple belongs
There are two tables. One is users info users, one is comments info comments.
in my database there are 3 tables CustomerType CusID EventType EventTypeID CustomerEventType CusID EventTypeID
SQL query which select the record from three tables and there is no relation
I've got collection of geo objects in database: There are four Tables: Countries Regions
One doubt in MSSQL. There are two tables in a databases. Table 1 named
alt text http://img502.imageshack.us/img502/7245/75088152.jpg There are two tables that I join them together, one of

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.