Posts

Showing posts with the label Recursive Conditional Calculation

SQL Server - Recursive Conditional Calculation.

I would like to share one SQL techniques to perform calculations recursively on SQL tables. This question is also very useful helpful in SQL Developer interview. Interviewer asked me below question. I have one table called CanalMaster. CanalMaster Contains all information related to Canal. Below are some information about CanalMaster table. There are two types of Canal Parent Canal - Doesn't have ParentCanalId Child Canal - have Canal Origin after Kilometer below is the SQL table. CREATE TABLE CanalMaster ( CanalId            numeric , -- Canal Unique Id CanalName          Varchar (100), -- Canal name ParentCanalId      numeric , -- Canal Parent Id CanalLength        numeric (10,2), -- Canal Length ChildCanalOriginKM numeric (10,2) -- Child Canal start origin from parent after kilometer. )    ...