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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:21:02+00:00 2026-05-28T16:21:02+00:00

Here’s a question I’ve been racking my brain over. Let’s say I have a

  • 0

Here’s a question I’ve been racking my brain over. Let’s say I have a table that has a series of timestamps and a part number as the primary key. The table stores incremental changes, meaning that for every timestamp, if a field changes, that change is recorded. If the field doesn’t change, then for the new timestamp it is NULL.
Here’s the basic idea.

 part | timestamp | x-pos | y-pos | status
------+-----------+-------+-------+--------
 a5   |       151 |     5 |    15 |      g
 a5   |       153 |  NULL |    17 |   NULL

(part, timestamp) is the primary key. The NULLs in the second record indicate values that are unchanged since the first record.

What I want to be able to do is select the most recent values for each field grouped by the part. For example, given the above entries, the results will be 153,5,17,g for part a5.

As of now, I have this hacked together query.

    ((SELECT x-pos FROM part_changes WHERE x-pos IS NOT NULL
    ORDER BY timestamp DESC
    LIMIT 1)

    UNION

    (SELECT y-pos FROM part_changesWHERE y-pos IS NOT NULL
    ORDER BY timestamp DESC
    LIMIT 1)

    UNION

    (SELECT status FROM part_changes WHERE status IS NOT NULL
    ORDER BY timestamp DESC
    LIMIT 1))

But this returns a single column, meaning that I can use a group-by for organizing.

There’s got to be a more elegant way of doing thing, such as using COALESCE or IS NULL in a creative way. But I’m stuck and can’t figure it out. Anybody got an idea?

And no, I can’t change the database structure.

EDIT: ruakh has the right idea. The only problem now is grouping by part. I can’t seem to get around the LIMIT 1 for grouping by multiple parts. Any ideas?

mdahlman, I’m not too familiar with analytic functions in postgresql. So, if that solution would be easier than a complex query, then by all means post your idea.

EDIT 2: Thank you all for the help. I think I’ve got a good enough grasp of what I need to do.

  • 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-28T16:21:04+00:00Added an answer on May 28, 2026 at 4:21 pm

    Rather than using a UNION, it sounds like you really want subqueries in the field list. That is, instead of (SELECT ...) UNION (SELECT ...) UNION (SELECT ...), you want SELECT (SELECT ...), (SELECT ...), (SELECT ...).


    For example:

    SELECT part,
           ( SELECT x_pos
               FROM part_changes
              WHERE part = pc.part
                AND x_pos IS NOT NULL
              ORDER
                 BY timestamp DESC
              LIMIT 1
           ) AS x_pos,
           ( SELECT y_pos
               FROM part_changes
              WHERE part = pc.part
                AND y_pos IS NOT NULL
              ORDER
                 BY timestamp DESC
              LIMIT 1
           ) AS y_pos,
           ( SELECT status
               FROM part_changes
              WHERE part = pc.part
                AND status IS NOT NULL
              ORDER
                 BY timestamp DESC
              LIMIT 1
           ) AS status
      FROM ( SELECT DISTINCT
                    part
               FROM part_changes
           ) AS pc
    ;
    

    But at this point I would really consider writing a stored procedure.


    Alternatively:

    SELECT DISTINCT
           part,
           FIRST_VALUE(x_pos) OVER
             ( PARTITION BY part
                   ORDER BY CASE WHEN x_pos IS NULL
                                 THEN NULL
                                 ELSE TIMESTAMP
                             END DESC NULLS LAST
             ) AS x_pos,
           FIRST_VALUE(y_pos) OVER
             ( PARTITION BY part
                   ORDER BY CASE WHEN y_pos IS NULL
                                 THEN NULL
                                 ELSE TIMESTAMP
                             END DESC NULLS LAST
             ) AS y_pos,
           FIRST_VALUE(status) OVER
             ( PARTITION BY part
                   ORDER BY CASE WHEN status IS NULL
                                 THEN NULL
                                 ELSE TIMESTAMP
                             END DESC NULLS LAST
             ) AS status
      FROM part_changes
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is my code (Say we have a single button on the page that
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see
Here an example of my checkbox list http://jsfiddle.net/YnM2f/ Let's say I check on G
Here's the scenario: I have a local git repository that mirrors the contents of
Here's the problem....I have three components...A Page that contains a User Control and a
Here's the situation, i want to have a user that can enter time on
Here is my problem...I have a page that loads a list of clients and

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.