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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:07:11+00:00 2026-06-03T03:07:11+00:00

A colleague of mine has a problem with a sql query:- Take the following

  • 0

A colleague of mine has a problem with a sql query:-

Take the following as an example, two temp tables:-

select 'John' as name,10 as value into #names
UNION ALL SELECT 'Abid',20 
UNION ALL SELECT 'Alyn',30 
UNION ALL SELECT 'Dave',15;

select 'John' as name,'SQL Expert' as job into #jobs
UNION ALL SELECT 'Alyn','Driver' 
UNION ALL SELECT 'Abid','Case Statement';

We run the following query on the tables to give us a joined resultset:-

select #names.name, #names.value, #jobs.job
FROM #names left outer join #jobs
on #names.name = #jobs.name

name    value    job
John    10       SQL Expert
Abid    20       Case Statement
Alyn    30       Driver
Dave    15       NULL

As ‘Dave’ does not exist in the #jobs table, he is given a NULL value as expected.

My colleague wants to modify the query so each NULL value is given the same value as the previous entry.

So the above would be:-

name    value    job
John    10       SQL Expert
Abid    20       Case Statement
Alyn    30       Driver
Dave    15       Driver

Note that Dave is now a ‘Driver’

There may be more than one NULL value in sequence,

name    value    job
John    10       SQL Expert
Abid    20       Case Statement
Alyn    30       Driver
Dave    15       NULL
Joe     15       NULL
Pete    15       NULL

In this case Dave, Joe and Pete should all be ‘Driver’, as ‘Driver’ is the last non null entry.

  • 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-03T03:07:12+00:00Added an answer on June 3, 2026 at 3:07 am

    There are probably better ways to do this. Here is one of the ways I could achieve the result using Common Table Expressions (CTE) and using that output to perform a OUTER APPLY to find the previous persion’s job. The query here uses id to sort the records and then determines what the previous person’s job was. You need at least one criteria to sort the records because data in tables are considered to be unordered sets.

    Also, the assumption is that the first person in the sequence should have a job. If the first person doesn’t have a job, then there is no value to pick from.

    Click here to view the demo in SQL Fiddle.

    Click here to view another demo in SQL Fiddle with second data set.

    Script:

     CREATE TABLE names
        (
                id      INT         NOT NULL IDENTITY
          ,     name    VARCHAR(20) NOT NULL
          ,     value   INT         NOT NULL
        );
    
        CREATE TABLE jobs
        (
                id  INT         NOT NULL
          ,     job VARCHAR(20) NOT NULL
        );
    
        INSERT INTO names (name, value) VALUES
          ('John', 10),
          ('Abid', 20),
          ('Alyn', 30),
          ('Dave', 40),
          ('Jill', 50),
          ('Jane', 60),
          ('Steve', 70);
    
        INSERT INTO jobs (id, job) VALUES
          (1, 'SQL Expert'),
          (2, 'Driver' ),
          (5, 'Engineer'),
          (6, 'Barrista');
    
        ;WITH empjobs AS
        (
            SELECT
            TOP 100 PERCENT n.id
                        ,   n.name
                        ,   n.value
                        ,   job
            FROM            names n 
            LEFT OUTER JOIN jobs j
            on              j.id = n.id
            ORDER BY        n.id
        ) 
        SELECT      e1.id
                ,   e1.name
                ,   e1.value
                ,   COALESCE(e1.job , e2.job) job FROM empjobs e1
        OUTER APPLY (
                      SELECT 
                      TOP 1     job 
                      FROM      empjobs     e2
                      WHERE     e2.id   < e1.id
                      AND       e2.job  IS NOT NULL
                      ORDER BY  e2.id   DESC
                    ) e2;
    

    Output:

    ID  NAME    VALUE  JOB
    --- ------  -----  -------------
    1   John      10   SQL Expert
    2   Abid      20   Driver
    3   Alyn      30   Driver
    4   Dave      40   Driver
    5   Jill      50   Engineer
    6   Jane      60   Barrista
    7   Steve     70   Barrista
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A colleague of mine has a problem which I am trying to help him
I have a problem that has just come up with a colleague of mine
A colleague of mine has a remote git repo that I wanted to clone
This may not be the typical stackoverflow question. A colleague of mine has been
A colleague of mine told me about a little piece of design he has
A colleague of mine (I promise it was a colleague!) has left an update
A colleague of mine says that SQL Server saves the date and time of
A colleague of mine has issues with his VPN connection. It seems that his
A colleague of mine is attempting to consume a web service that has been
One colleague of mine has troubles during the DllMain Detach process. His bug seems

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.