Hierarchical tree data in sql ,

 



The given query is a recursive common table expression (CTE) that retrieves a hierarchical tree structure of filter types and their corresponding options based on a given product ID. Here's a breakdown of the query:


The query starts by defining a CTE named tree_cte which serves as a recursive anchor. It selects the initial set of filter types (FilterTypes) that are associated with the given product ID (@ProductId).


The anchor part of the CTE includes the columns Id, ParentFilterTypeId, and LabelName from the FilterTypes table. These columns represent the filter type's unique identifier, its parent filter type ID (if any), and its label name.


The WHERE clause in the anchor part filters the initial set of filter types based on their ID. It uses a subquery to retrieve the FilterTypeId values from the ProductFilterTypeOptions table where the ProductId matches the given parameter value.


The UNION ALL part of the CTE is the recursive step. It joins the FilterTypes table (td) with the tree_cte based on the ParentFilterTypeId column. This ensures that filter types with a parent filter type ID found in the previous iteration are included in the result.


The main SELECT statement outside the CTE is used to retrieve the final result. It selects columns from tree_cte and joins the FilterTypeOptions table (FTO) based on the FilterTypeId column.


The selected columns in the final result include:


FilterTypeId: The unique identifier of the filter type.

ParentFilterTypeId: The unique identifier of the parent filter type.

FilterTypeName: The label name of the filter type.

FilterTypeOptionsId: The unique identifier of the filter type option.

FilterTypeOptionsName: The name of the filter type option.

Color: The color associated with the filter type option.

Description: The description of the filter type option.

Icon: The icon associated with the filter type option.

SelectionType: A fixed value of 0 (presumably indicating a default selection type).

Please note that without additional context or information about the database schema and sample data, it is not possible to provide a more specific description of the query result or its purpose.


 

WITH tree_cte AS (

  SELECT FT.Id, FT.ParentFilterTypeId, FT.LabelName

  FROM FilterTypes FT

 WHERE 

--FT.id in ('')

  FT.id  in (SELECT FilterTypeId FROM ProductFilterTypeOptions WHERE ProductId = @ProductId)

  UNION ALL

  SELECT td.Id, td.ParentFilterTypeId, td.LabelName

  FROM FilterTypes td

  JOIN tree_cte ON td.ParentFilterTypeId = tree_cte.id


)

SELECT Convert( nvarchar(50),tree_cte.Id) FilterTypeId, Convert( nvarchar(50),tree_cte.ParentFilterTypeId)ParentFilterTypeId, tree_cte.LabelName as FilterTypeName,Convert( nvarchar(50),FTO.id)FilterTypeOptionsId , FTO.Name as FilterTypeOptionsName, FTO.Color,FTO.Description,FTO.Icon,0 as SelectionType

FROM tree_cte 

LEFT JOIN FilterTypeOptions FTO ON FTO.FilterTypeId = tree_cte.Id




Post a Comment

Previous Post Next Post