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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:18:07+00:00 2026-05-28T05:18:07+00:00

I have a table that looks like something like this: timestamp value person ===============================================

  • 0

I have a table that looks like something like this:

timestamp                value           person
===============================================
2010-01-12 00:00:00       33              emp1
2010-01-12 11:00:00       22              emp1
2010-01-12 09:00:00       16              emp2
2010-01-12 08:00:00       16              emp2
2010-01-12 12:12:00       45              emp3
2010-01-12 13:44:00       64              emp4
2010-01-12 06:00:00       33              emp1
2010-01-12 15:00:00       12              emp5

I wanted to find the maximum value associated with each person. The obvious query was:

select person,max(value) from table group by person

Now I wanted to include the timestamp associated with each max(value). I could not use timestamp column in the above query because as everyone knows, it won’t appear in the group by clause. So I wrote this instead:

select x.timestamp,x.value,x.person from table as x,
(select person,max(value) as maxvalue from table group by person order by maxvalue 
 desc) as y
where x.person = y.person
and x.value = y.maxvalue

This works — to an extent. I now see:

timestamp                value           person
===============================================
2010-01-12 13:44:00       64              emp4
2010-01-12 12:12:00       45              emp3
2010-01-12 06:00:00       33              emp1
2010-01-12 00:00:00       33              emp1
2010-01-12 08:00:00       16              emp2
2010-01-12 09:00:00       16              emp2
2010-01-12 15:00:00       12              emp5

The problem is now I get all the entries for emp1 and emp2 that ends up with the same max(value).

Suppose among emp1 and emp2, I only want to see the entry with the latest timestamp. IOW, I want this:

timestamp                value           person
===============================================
2010-01-12 13:44:00       64              emp4
2010-01-12 12:12:00       45              emp3
2010-01-12 06:00:00       33              emp1
2010-01-12 09:00:00       16              emp2
2010-01-12 15:00:00       12              emp5

What kind of query would I have to write? Is it possible to extend the nested query I wrote to achieve what I want or does one have to rewrite everything from the scratch?

If its important, because I am using Sqlite, timestamps are actually stored as julian days. I use the datetime() function to convert them back to a string representation in every query.

  • 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-28T05:18:08+00:00Added an answer on May 28, 2026 at 5:18 am

    You were almost there:

    SELECT max(x.timestamp) AS timestamp, x.value, x.person
         , y.max_value, y.ct_value, y.avg_value
    FROM   table AS x
    JOIN  (
        SELECT person
             , max(value) as max_value
             , count(value) as ct_value
             , avg(value) as avg_value
        FROM   table
        GROUP  BY person
        ) AS y ON (x.person, x.value) = (y.person, y.max_value)
    GROUP BY x.person, x.value, y.max_value, y.ct_value, y.avg_value
    -- ORDER  BY x.person, x.value
    

    You cannot compute max(x.timestamp) in the same nested query, because you don’t want the absolute maximum per person, but the one accompanying the maximum value. So you have to aggregate another time on the next query level.

    Compute max(x.timestamp) before you convert it to its string representation – though your format would sort correctly, too. But that that should perform better.

    Note how I transformed your cross join with where conditions to an [inner] join with a (simplified) join condition. Does the same, just more like the canonical way of the SQL standard and more readable.

    All of this could be done in one query level with window functions (max() and first_value()), which are implemented in all the bigger RDBMS (except MYSQL), but not in SQLite.


    Edit

    Included additional aggregates after request in comment.

    • 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 looks something like this: ________________________ |id|value|date|approved| ----------------------- What I
I have a table that looks something like this: word big expensive smart fast
I have a table that looks something like this: id | firstperson | secondperson
I currently have a table structure that looks something like this(some details omitted): ColumnName
I have a table that looks something like this: CREATE TABLE student_results(id integer, name
So lets say I have a table that looks something like this: ItemName ProductType
I have a table that looks like this: id datatype name value 0 nvarchar(255)
I have a table that looks something like this CREATE TABLE MyTable( [RecordID] [bigint]
I have a table that looks something like this. <table> <thead> <tr> <th>Foo</th> <th>Bar</th>
I have a table that looks something like this: pageload --pageloadid (autoincrementing numeric id

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.