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 – Generate the data in the RANGE format

SQL Puzzle: SQL Advance Query – Generate the data in the RANGE format

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

Check the below input data and expected output to start the range from 0 to first value and the first value to second value so on.

Input Data:

1
2
3
4
5
6
7
8
Val
-----------
108
116
226
308
416
426

Expected Data:

1
2
3
4
5
6
7
8
9
LowerRange UpperRange
----------- -----------
0 108
108 116
116 226
226 308
308 416
416 426
426 NULL

Create a table with sample data:

1
2
3
4
5
6
7
8
9
CREATE TABLE Ranges
(Val INT)
GO
INSERT INTO Ranges(Val)
VALUES
(108),(116),
(226),(308),
(416),(426)

Solution 1:

1
2
3
4
5
6
7
;with CTE as
(
select *,row_number()over (order by Val) as rnk from (select 0 Val union select Val from Ranges) AS T
)
select a.Val as LowerRange,b.Val as UpperRange
from CTE a
left outer join CTE b on a.rnk = b.rnk-1

Solution 2:

1
2
3
4
5
6
7
8
9
10
11
;WITH CTE AS
(
SELECT Val , ROW_NUMBER() OVER (ORDER BY Val ASC) rnk FROM
(
SELECT 0 Val
UNION ALL
SELECT Val from Ranges
)a
)
SELECT c.Val LowerRange , (SELECT TOP 1 Val FROM CTE c1 WHERE c1.rnk > c.rnk ) UpperRange
FROM CTE c

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

Mar 11, 2019Anvesh Patel
PostgreSQL: Bash Shell Script to execute psql command in UNIX / LINUXSQL Puzzle: SQL Advance Query - Replace NULL with Previous Non Null value
Comments: 5
  1. HIC
    April 6, 2019 at 7:58 pm

    with a as (
    select rownum as id, val
    from ranges
    )
    , b as (
    select (rownum + 1) as id, val
    from ranges
    )
    select nvl(b.val, 0) as LowerRange, a.val as UpperRange
    from a
    full join b on b.id = a.id
    order by LowerRange;

    ReplyCancel
  2. Sai Nath
    August 11, 2019 at 8:13 am

    ; WITH CTE AS
    (
    SELECT
    *,
    ROW_NUMBER() OVER( ORDER BY (SELECT 1)) AS RW
    FROM
    Ranges
    )

    SELECT
    A.VAL,
    B.VAL
    FROM
    CTE AS A
    LEFT JOIN
    CTE AS B ON A.RW = B.RW-1

    UNION

    SELECT
    B.VAL,
    A.VAL
    FROM
    CTE AS A
    LEFT JOIN
    CTE AS B ON A.RW-1 = B.RW

    ReplyCancel
  3. koustav modak
    October 30, 2019 at 12:46 pm

    (select a1.val LowerRange ,a2.val UpperRange from
    (select row_number() over() as rnk,a.* from
    (select * from
    (select 0 val from Ranges
    union
    select Val from Ranges)T
    order by 1)a )a1,
    (select row_number() over() as rnk,a.* from
    (select * from
    (select 0 val from Ranges
    union
    select Val from Ranges)T
    order by 1)a )a2
    where a1.rnk+1=a2.rnk)
    union all
    (select Val,null from Ranges order by 1 desc limit 1)

    ReplyCancel
  4. sohail
    November 2, 2019 at 6:38 am

    select val , LEAD(val) over (order by val)
    from
    (select 0 as val union select val from Ranges ) as t

    ReplyCancel
  5. Hareesh
    January 3, 2020 at 10:23 am

    with CTE as (select 0 as Val
    union
    select Val from Ranges)
    select Val as [Lower Range], LEAD(Val) over (order by val) as [Upper Range] from CTE

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

March 11, 2019 5 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, RANGE, 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....