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 2017 August SQL Puzzle: SQL Advance Query – Find the shortest path between two Roads

SQL Puzzle: SQL Advance Query – Find the shortest path between two Roads

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

Check the below input data and expected output to find the shortest path between the given two roads. (For example, find shortest path between road NH220 to NH320)

Input Data:

1
2
3
4
5
6
7
8
9
10
ID FromRoadNo ToRoadNo Distance
----------- ---------- -------- -----------
1 NH108 NH100 50
2 NH100 NH120 70
3 NH108 NH120 100
4 NH110 NH220 90
5 NH108 NH110 60
6 NH220 NH150 70
7 NH150 NH320 30
8 NH220 NH320 150

Expected Output: For shortest path between road NH220 to NH320

1
2
3
RequireRoads TotalDistance
------------------------ -------------
.NH220.NH150..NH320. 100

Create a table with data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CREATE TABLE Roads
(
ID INT
,FromRoadNo CHAR(5)
,ToRoadNo CHAR(5)
,Distance INT
)
GO
INSERT INTO Roads VALUES
(1,'NH108','NH100',50)
,(2,'NH100','NH120',70)
,(3,'NH108','NH120',100)
,(4,'NH110','NH220',90)
,(5,'NH108','NH110',60)
,(6,'NH220','NH150',70)
,(7,'NH150','NH320',30)
,(8,'NH220','NH320',150)
GO

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
DECLARE @StartingRoadNo AS VARCHAR(50) = 'NH220'
DECLARE @EndingRoadNo AS VARCHAR(50) = 'NH320'
;WITH CTE AS
(
SELECT ToRoadNo ,
CASE WHEN FromRoadNo IS NULL THEN CAST('.'+ISNULL(FromRoadNo,ToRoadNo)+'.' AS VARCHAR(MAX))
WHEN FromRoadNo IS NOT NULL THEN CAST('.'+FromRoadNo+'.'+ToRoadNo+'.' AS VARCHAR(MAX))
END RequireRoads
, Distance TotalDistance
FROM Roads WHERE ( FromRoadNo = @StartingRoadNo )
UNION ALL
SELECT a.ToRoadNo , c.RequireRoads+'.'+a.ToRoadNo+'.' RequireRoads
,TotalDistance + a.Distance TotalDistance
FROM Roads a
INNER JOIN CTE c ON a.FromRoadNo = c.ToRoadNo
)
,CTE2 AS
(
SELECT * , RANK() OVER (ORDER BY TotalDistance) rnk FROM CTE
WHERE ToRoadNo = @EndingRoadNo AND PATINDEX('%'+@EndingRoadNo+'%',RequireRoads) > 0
)
SELECT RequireRoads, TotalDistance
FROM CTE2 WHERE rnk = 1

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

Aug 26, 2017Anvesh Patel
SQL Server: Use sp_clean_db_free_space to remove Ghost Records or Dead TuplesSQL Server: Use sp_recompile to recompile the stored procedures, triggers, and user-defined functions
Comments: 4
  1. NAVEEN
    October 12, 2017 at 10:10 am

    select TOP 1 A.DISTANCE+ISNULL(B.DISTANCE,0)SUM1,CONCAT(A.FromRoadNo,’..’,A.TOROADNO,’..’,ISNULL(B.TOROADNO,”)) from roads a
    left join roads b
    on a.toRoadNo=b.FromRoadNo
    where a.fromroadno=’NH220′ AND ( A.toroadno=’NH320′ OR B.TOROADNO=’NH320′)
    ORDER BY 1

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      October 12, 2017 at 10:57 am

      thanks for sharing alternative, but try to do without any hard-code value like NH220.

      ReplyCancel
  2. Dinesh.IS
    October 12, 2017 at 3:03 pm

    DECLARE @StartingRoadNo AS VARCHAR(50) = ‘NH108’
    DECLARE @EndingRoadNo AS VARCHAR(50) = ‘NH120′

    ;
    WITH CTE
    As
    (Select Row_Number()Over(Order By ToRoadNo) As SLNo,Row_Number()Over(Partition By ToRoadNo Order By ToRoadNo Desc) PSLNo,FromRoadNo,ToRoadNo,Distance From Roads Where ((FromRoadNo=@StartingRoadNo) Or (ToRoadNo Between @EndingRoadNo And @EndingRoadNo))),
    N
    As
    (Select PSLNo,SLNo,FromRoadNo,ToRoadNo,Distance From CTE),
    F
    As
    (Select PSLNo,Sum(IsNull(Distance,0)) As Distance From N
    Group By PSLNo)
    Select @StartingRoadNo+’,’+(Select Convert(VarChar(4000),ToRoadNo)+’,’ From N Where N.PSLNo=F.PSLNo For XML PATH(”)) As Traval,Distance
    From F Where F.Distance=(Select Min(Distance) From F)

    ReplyCancel
  3. Dinesh.IS
    October 12, 2017 at 3:08 pm

    —Method-2
    DECLARE @StartingRoadNo AS VARCHAR(50) = ‘NH220’
    DECLARE @EndingRoadNo AS VARCHAR(50) = ‘NH320′

    ;
    WITH CTE
    As
    (Select Row_Number()Over(Order By ToRoadNo) As SLNo,Row_Number()Over(Partition By ToRoadNo Order By ToRoadNo Desc) PSLNo,
    FromRoadNo,ToRoadNo,Distance From Roads Where ((FromRoadNo=@StartingRoadNo) Or (ToRoadNo Between @EndingRoadNo And @EndingRoadNo))),
    N
    As
    (Select PSLNo,Sum(IsNull(Distance,0)) Distance From CTE
    Group By PSLNo)

    Select @StartingRoadNo+(Select Convert(VarChar(500),ToRoadNo)+’,’ From CTE B Where B.PSLNo=N.PSLNo For XML PATH(”)) As Traval,Distance
    From N Where Distance=(Select Min(Distance) From N)

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

August 26, 2017 4 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....