【问题标题】:Parent row missing in child parent relationship in with CTE与 CTE 的子父关系中缺少父行
【发布时间】:2012-02-07 07:28:59
【问题描述】:

我有一个临时表,其中有以下数据,我想用他的父级 categoryID 过滤子级的行,直到它到达那些层次结构中父级的顶部。

;with cte (rowid,ParentCategoryID,CategoryID,Status,Level,CategoryName,ISProduct) as
(
     Select  rowid,ParentCategoryID,CategoryID,Status,Level,CategoryName,ISProduct from #newtemp      where ParentCategoryId!=0

     union all
     select cte.rowid,cte.ParentCategoryID,cte.CategoryID,cte.Status,cte.Level,cte.CategoryName,cte.ISProduct
     from  #newtemp inner join cte ON cte.CategoryId=#newtemp.ParentCategoryId
)
select * from cte

【问题讨论】:

  • 你有什么问题?结果是什么?
  • 我希望 'ghfd' 的父级 'f' 应该在结果表中。这不会出现

标签: sql-server-2005 parent-child common-table-expression


【解决方案1】:

您需要将 cte.CategoryId=#newtemp.ParentCategoryId 替换为 c.ParentCategoryId = #newtemp.CategoryID

;with cte (rowid,ParentCategoryID,CategoryID,Status,Level,CategoryName,ISProduct) as
 (
  Select  rowid, ParentCategoryID, CategoryID, Status, Level, CategoryName, ISProduct 
  from #newtemp  
  where ParentCategoryId!=0
  union all
  select t.rowid, t.ParentCategoryID, t.CategoryID, t.Status, t.Level, t.CategoryName, t.ISProduct
  from  #newtemp t inner join cte c ON c.ParentCategoryId = t.CategoryID
  )
  select * from cte

SQLFiddle上的演示

【讨论】:

    【解决方案2】:

    如果我理解你是正确的。你是这样的:

    首先是一些测试数据:

    DECLARE @tbl TABLE
                (
                    rowid INT,
                    parentCategoryID INT,
                    CategoryID INT,
                    [Status] INT,
                    [Level] INT, 
                    CategoryName VARCHAR(100),
                    ISProduct BIT
                )
    
    INSERT INTO @tbl
    SELECT 1,0,1,1,0,'jewellary',1 UNION ALL
    SELECT 2,0,2,1,0,'f',0 UNION ALL
    SELECT 11,2,4,1,10,'ghdf',1
    

    然后是这样的 CTE:

    ;WITH cte_name (rowid,CategoryID,parentCategoryID,HasChildren)
    AS
    (
    SELECT
        tbl.rowid,
        tbl.CategoryID,
        tbl.parentCategoryID,
        CASE WHEN EXISTS 
                (
                    SELECT
                        NULL
                    FROM
                        @tbl AS tblInner
                    WHERE
                        tblInner.parentCategoryID=tbl.CategoryID
                )
            THEN 1
            ELSE 0
        END
        AS HasChildren
    FROM
        @tbl AS tbl
    WHERE
        tbl.parentCategoryID=0
    UNION ALL
    SELECT
        tbl.rowid,
        tbl.CategoryID,
        tbl.parentCategoryID,
        cte.HasChildren
    FROM
        @tbl AS tbl
        JOIN cte_name AS cte
            on cte.CategoryID=tbl.parentCategoryID
    )
    
    SELECT 
        tbl.*
    FROM 
        cte_name
        JOIN @tbl AS tbl
            ON cte_name.rowid=tbl.rowid
    WHERE
        cte_name.HasChildren=1
    

    【讨论】:

      猜你喜欢
      • 2014-07-01
      • 1970-01-01
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      相关资源
      最近更新 更多