Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2019 March SQL Puzzle: SQL Advance Query – Replace NULL with Previous Non Null value

SQL Puzzle: SQL Advance Query – Replace NULL with Previous Non Null value

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output to replace the NULL value with the previous NON-Null value.

Input Data:

1
2
3
4
5
6
7
8
9
10
Id Code
----------- ----
1 ABC
2 XYZ
3 NULL
4 NULL
5 PQR
6 NULL
7 ERT
8 NULL

Expected Output:

1
2
3
4
5
6
7
8
9
10
Id Code Previous_NonNullCode
----------- ---- --------------------
1 ABC ABC
2 XYZ XYZ
3 NULL XYZ
4 NULL XYZ
5 PQR PQR
6 NULL PQR
7 ERT ERT
8 NULL ERT

Create a table with data:

1
2
3
4
5
6
7
8
9
10
11
12
CREATE TABLE tbl_Values
(
Id INT
,Code VARCHAR(3)
)
GO
 
INSERT INTO tbl_Values
VALUES
(1,'ABC'),(2,'XYZ'),(3,NULL),(4,NULL)
,(5,'PQR'),(6,NULL),(7,'ERT'),(8,NULL)
GO

Solution:

1
2
3
4
5
6
7
8
9
10
11
;WITH CTE AS
(
SELECT *,ROW_NUMBER() OVER (ORDER BY Id) rnk
FROM tbl_Values
)
SELECT
Id
,Code
,(SELECT TOP 1 Code FROM CTE a WHERE a.rnk =
(SELECT MAX(b.rnk) FROM CTE b WHERE b.rnk <= c.rnk AND b.Code IS NOT NULL)) Previous_NonNullCode
FROM CTE c

Please try the different solution for this puzzle and share it via comment...

Mar 18, 2019Anvesh Patel
SQL Puzzle: SQL Advance Query - Generate the data in the RANGE formatPostgreSQL: Script to convert User to Super User
Comments: 5
  1. Ram Chekuri
    April 1, 2019 at 12:34 am

    with x as (
    select *, sum(case when code is null then 0 else 1 end) over (order by id) rnk
    from tbl_values
    )

    select id, code, max(code) over (partition by rnk)
    from x

    ReplyCancel
  2. HIC
    April 6, 2019 at 8:00 pm

    select
    a.id
    , a.code
    , nvl(a.code, (select b.code from tbl_Values b where b.id = (select max(c.id) from tbl_Values c where c.id < a.id and c.code is not null)))
    from
    tbl_Values a
    order by id

    ReplyCancel
  3. Gregor
    June 4, 2019 at 10:59 am

    select
    *,
    (case
    when Code is not null then Code
    else
    (select top 1 innertbl.Code
    from tbl_Values innertbl
    where innertbl.Code is not null
    and
    innertbl.Id <= outertbl.Id
    order by Id desc)
    end)
    as Previous_NonNullCode
    from tbl_Values outertbl

    ReplyCancel
  4. Phanindra Suripeddi
    August 19, 2019 at 9:50 am

    SELECT Id,Code, IIF(Code IS null, (SELECT TOP 1 b.Code FROM tbl_Values b WHERE b.id<a.id AND b.Code IS NOT NULL ORDER BY b.id DESC),code) Previous_NonNullCode FROM tbl_Values a

    ReplyCancel
  5. koustav
    October 30, 2019 at 11:51 am

    select *,
    (select code from tbl_Values where id in(
    (select max(id) from tbl_Values tbl1 where tbl1.id<=tbl.id and code is not null)))

    from tbl_Values tbl

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

March 18, 2019 5 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....