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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:36:41+00:00 2026-06-14T10:36:41+00:00

This is an extended version of a related previous question . I have posted

  • 0

This is an extended version of a related previous question. I have posted it a new question for Erwin Brandstetter suggested me to do so. (I realized that I actually wanted this, after people replied to my first question)

Having the following data (blank means NULL):

ID    User  ColA    ColB    ColC
1     1     15              20
2     1     11      4       
3     1             3
4     2     5       5       10
5     2     6 
6     2             8
7     1             1

How can I get the last not-NULL values of each column for all users, the simplest way? So the resulting for the given data would be:

User  ColA    ColB    ColC
1     11      1       20
2     6       8       10

I have not found much, the function that seemed to do something similar to what I describe was COALESCE, but it does not work as expected in my case.

Note: Standard SQL if possible, PostgreSQL otherwise. The count of the involved columns might change, so a solution that is not tied to these three specific columns would be best.

  • 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-14T10:36:42+00:00Added an answer on June 14, 2026 at 10:36 am

    "Standard" SQL

    Similar to what I posted on the previous question, a recursive CTE is elegant and probably the fastest way to do it in standard SQL – especially for many rows per user.

    WITH RECURSIVE t AS (
       SELECT row_number() OVER (PARTITION BY usr ORDER  BY id DESC) AS rn
             ,usr, cola, colb, colc
       FROM   tbl
       )
    
       , x AS (
       SELECT rn, usr, cola, colb, colc
       FROM   t
       WHERE  rn = 1
    
       UNION ALL
       SELECT t.rn, t.usr
            , COALESCE(x.cola, t.cola)
            , COALESCE(x.colb, t.colb)
            , COALESCE(x.colc, t.colc)
       FROM   x
       JOIN   t USING (usr)
       WHERE  t.rn = x.rn + 1
       AND    (x.cola IS NULL OR x.colb IS NULL OR x.colc IS NULL)
       )
    SELECT DISTINCT ON (usr)
           usr, cola, colb, colc
    FROM   x
    ORDER  BY usr, rn DESC;
    

    -> sqlfiddle for requested PostgreSQL.

    The only non-standard element is DISTINCT ON, which is an extension to DISTINCT in the standard. Replace the final SELECT with this for a standard SQL:

    SELECT usr
          ,max(cola) As cola
          ,max(colb) As colb
          ,max(colc) As colc
    FROM   x
    GROUP  BY usr
    ORDER  BY usr;
    

    The request for "standard SQL" is of limited use. The standard only exists on paper. No RDBMS implements 100 % standard SQL – it would be rather pointless, too, since the standard includes nonsensical parts here and there. Arguably, PostgreSQL’s implementation is among the closest to the standard.

    PL/pgSQL function

    This solution is specific to PostgreSQL, but should perform very well.

    I am building on the same table as demonstrated in the fiddle above.

    CREATE OR REPLACE FUNCTION f_last_nonull_per_user()
    RETURNS SETOF tbl AS
    $func$
    DECLARE
       _row tbl;  -- table name can be used as row type
       _new tbl;
    BEGIN
    
    FOR _new IN
       SELECT * FROM tbl ORDER BY usr, id DESC
    LOOP
       IF _new.usr = _row.usr THEN 
          _row.id := _new.id;   -- copy only id
          IF _row.cola IS NULL AND _new.cola IS NOT NULL THEN
             _row.cola := _new.cola; END IF;   -- only if no value found yet
          IF _row.colb IS NULL AND _new.colb IS NOT NULL THEN
             _row.colb := _new.colb; END IF;
          IF _row.colc IS NULL AND _new.colc IS NOT NULL THEN
             _row.colc := _new.colc; END IF;
       ELSE
          IF _new.usr <> _row.usr THEN  -- doesn't fire on first row
             RETURN NEXT _row;
          END IF;   
          _row := _new;  -- remember row for next iteration
       END IF;
    END LOOP;
    
    RETURN NEXT _row;  -- return row for last usr
    
    END
    $func$ LANGUAGE plpgsql;
    

    Call:

    SELECT * FROM f_last_nonull_per_user();
    

    Returns the whole row – including the min id we need to fill all columns.

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

Sidebar

Related Questions

This question is extended part of my previous question, Finding number position in string
I have a MapView that will contain alot of overlays. For this i've extended
Looks like this question is popular, but there is no jQuery version. I have
We have a WinForms control that is an extended version of ComboBox that supports
This is actually extended version of Alternatives to using an activity for each tab
I am all very new to this and am trying to use extended hex
This is not a question of the new features in enum. This is not
Yes, I know. This question have been already replied in Where to store the
I am working on a project that is built on an extended version of
This may be a bit of an odd question, and what I have in

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.