Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Towards the innovative SQL ideas
Browse by Tags · View All
sql server 123
sql 122
ms sql server 119
ms sql 118
database 107
tsql 81
#SQL Server 78
t-sql 75
#sql 71
sql server general 67

Archive · View All
April 2011 14
July 2011 12
May 2011 12
August 2011 11
June 2011 10
September 2011 8
December 2011 6
November 2011 6
June 2013 5
April 2013 5

Hierarchical data using CTE (Common Table Expression) with Depth First Strategy - SQL Server

May 22 2011 12:00AM by Paresh Prajapati   

CTE (Common Table Expression) is best feature to get the hierarchy of the data and hierarchical depth. Using CTE we can also get the data easily by level wise with parent and child relation.

Recently when i working with stored procedure, at that time i was need to develop the logic to get the data in hierarchical order. I did it with CTE easily. Today i am going to demonstrate CTE with small examples.

Creating Objects
IF ( Object_id('CTEMaxLevelTab') > 0 )
  DROP TABLE CTEMaxLevelTab

GO

CREATE TABLE CTEMaxLevelTab
  (
     CategotyId        INT
     ,ParentCategotyId INT
     ,CategotyOrder INT
     ,CategotyName     VARCHAR(20)
  ) 

GO

-- Inserting samples records
INSERT INTO CTEMaxLevelTab
 (
 CategotyId
 ,ParentCategotyId
 ,CategotyOrder
 ,CategotyName
 )
values
  ( 1,NULL,1,'Category - 1'),
  ( 2,1,1,'Category - 2'),
  ( 3,2,1,'Category - 3'),
  ( 4,3,1,'Category - 4'),
  ( 5,4,1,'Category - 5'),
  ( 6,2,2,'Category - 6'),
  ( 7,6,1,'Category - 7'),
  ( 8,1,2,'Category - 8')
GO 

-- Viewing data in table 
SELECT
  *
FROM   CTEMaxLevelTab

GO 

Let us look for the below script which will return the data in level Order,
-- Creating CTE
;WITH cteLevels
AS
(
SELECT
  CategotyId   AS Child
  ,ParentCategotyId AS Parent
  ,CategotyOrder as CategotyOrder
  ,1        AS [Level],
  CAST (REPLICATE('.',1) + CategotyName as varchar(25) )  as [Categoty Name]
FROM   CTEMaxLevelTab 
WHERE CategotyId = 1


UNION ALL

SELECT
  CategotyId      AS Child
  ,ParentCategotyId    AS Parent
  ,CTEMaxLevelTab.CategotyOrder as CategotyOrder
  ,[Level] + 1 AS [Level],
   CAST (REPLICATE('.',[Level] + 1) + CategotyName as varchar(25) )  as [Categoty Name]
FROM   CTEMaxLevelTab
       INNER JOIN cteLevels
         ON ( ParentCategotyId = Child ) 

)

-- Viewing Data
SELECT
  *
FROM   cteLevels

GO



Below script which will return the data in hierarchical order,
-- Creating CTE
;WITH cteLevels
AS
(
SELECT
  CategotyId                                              AS Child
  ,ParentCategotyId                                       AS Parent
  ,CategotyOrder                                          AS CategotyOrder
  ,1                                                      AS [Level]
  ,CAST(CategotyId AS VARCHAR(MAX))                       AS [Order]
  ,CAST (Replicate('.', 1) + CategotyName AS VARCHAR(25)) AS [Categoty Name]
FROM   CTEMaxLevelTab
WHERE  CategotyId = 1 


UNION ALL

SELECT
  CategotyId                                                        AS Child
  ,ParentCategotyId                                                 AS Parent
  ,CTEMaxLevelTab.CategotyOrder                                     AS CategotyOrder
  ,[Level] + 1                                                      AS [Level]
  ,[Order] + '.' + CAST(CategotyId AS VARCHAR(MAX))                 AS [Order]
  ,CAST (Replicate('.', [Level] + 1) + CategotyName AS VARCHAR(25)) AS [Categoty Name]
FROM   CTEMaxLevelTab
       INNER JOIN cteLevels
         ON ( ParentCategotyId = Child ) 

)

-- Viewing Data
SELECT
  *
FROM   cteLevels  
order by [Order]; 

GO


You can see how CTE can help to get the data and the hierarchical relation of records.

Get easy with CTE!

Tags: sql, tsql, sql server, ms sql, ms sql server, t-sql, #SQL Server, mssql, #sql, database, sql server general, cte,


Paresh Prajapati
6 · 22% · 7102
4
 
 
0
Refreshed
 
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Hierarchical data using CTE (Common Table Expression) with Depth First Strategy - SQL Server" rated 5 out of 5 by 4 readers
Hierarchical data using CTE (Common Table Expression) with Depth First Strategy - SQL Server , 5.0 out of 5 based on 4 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]