My colleague has a problem with sql query: -
As an example, take two temporary tables as an example: -
select 'John' as name,10 as value into
UNION ALL SELECT 'Abid',20
UNION ALL SELECT 'Alyn',30
UNION ALL SELECT 'Dave',15;
select 'John' as name,'SQL Expert' as job into
UNION ALL SELECT 'Alyn','Driver'
UNION ALL SELECT 'Abid','Case Statement';
We run the following query in the tables to give us a combined set of results: -
select
FROM
on
name value job
John 10 SQL Expert
Abid 20 Case Statement
Alyn 30 Driver
Dave 15 NULL
Since "Dave" does not exist in the #jobs table, it is set to NULL as expected.
My colleague wants to modify the query, so each NULL value is assigned the same value as the previous record.
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"
A sequence can have more than one NULL value,
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 be “Driver”, since “Driver” is the last non-empty record.