Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL-在具有某些条件的层次结构中标识parentid的查询_Sql_Sql Server - Fatal编程技术网

SQL-在具有某些条件的层次结构中标识parentid的查询

SQL-在具有某些条件的层次结构中标识parentid的查询,sql,sql-server,Sql,Sql Server,我在SQL azure数据库中有一个表,其中包含父级的层次结构,我希望在父级的总体层次结构中标识满足某些条件的每个项的父级id 作为一个例子,这可能是一个很好的例子 categoryId parentId Typeid 1 null 1 2 1 2 3 2 3 4 3 4 5 3 4 6

我在SQL azure数据库中有一个表,其中包含父级的层次结构,我希望在父级的总体层次结构中标识满足某些条件的每个项的父级id

作为一个例子,这可能是一个很好的例子

categoryId  parentId    Typeid
1           null         1
2           1            2
3           2            3
4           3            4
5           3            4
6           null         5
7           6            1
8           7            2
9           8            3
10          9            4
11          9            4
我想为每个categoryID找到层次结构中的ParentId,类型是1,如果这个类别是类型1,那么把它放在同一个类别中,以便接收类似的内容

categoryId  parentId     Typeid   ParentSearch
1           null         1        1
2           1            2        1
3           2            3        1
4           3            4        1
5           3            4        1
6           null         5        null
7           6            1        7
8           7            2        7
9           8            3        7
10          9            4        7
11          9            4        7
正如您可以看到的从1到5的所有类别一样,类型为1的父级是类别1 对于从6到11的类别,6需要为null,其余的父类为7

有可能吗? 也许是创造了一条路或者什么的。我已经做了几个左连接在leves,这是很好的,但我不想创建500 leves,以确保我们从来没有500级儿童

谢谢和问候

A是你的朋友:

WITH parents AS
 (SELECT categoryid, parentid, typeid
       , CASE WHEN typeid = 1 THEN categoryid ELSE NULL END AS parentsearch
  FROM mytable
  WHERE parentid IS NULL
 UNION ALL
  SELECT c.categoryid, c.parentid, c.typeid
       , coalesce(p.parentsearch, CASE WHEN c.typeid = 1 THEN c.categoryid ELSE null END)
  FROM mytable AS c
  JOIN parents AS p ON c.parentid = p.categoryid)
SELECT * FROM parents ORDER BY categoryid;
categoryid parentid typeid parentsearch
----------  ----------  ----------  ------------
1(空)1 1
2           1           2           1           
3           2           3           1           
4           3           4           1           
5           3           4           1           
6(零)5(零)
7           6           1           7           
8           7           2           7           
9           8           3           7           
10          9           4           7           
11          9           4           7     

.

另一种使用递归CTE的解决方案。
但这颗种子来自孩子们

WITH RCTE AS
(
    SELECT t.categoryId, t.parentId, t.typeId, 
    0 as lvl,
    t.categoryId as nextCategoryId,
    t.parentId as nextParentId,
    t.typeId as nextTypeId
    FROM CategoryRelations t

    UNION ALL

    SELECT c.categoryId, c.parentId, c.typeId, 
    c.lvl + 1, 
    t.categoryId,
    t.parentId,
    t.typeId
    FROM RCTE c
    JOIN CategoryRelations t ON t.categoryId = c.nextParentId
    WHERE c.typeId != 1
)
SELECT
 c.categoryId,
 c.parentId, 
 c.typeId,
 (case when c.nextTypeId = 1 then c.nextCategoryId end) as ParentSearch
FROM RCTE c
WHERE (c.nextTypeId = 1 or c.parentId is null)
ORDER BY categoryId;

在rextester上进行测试

您使用哪个数据库?你能用itI标记这个问题吗?我不太明白逻辑到底是什么,请接受这样一个事实:它看起来像是你在遍历每个父级,直到parentId为null,在这种情况下,它是一个“根父级”(id 1和6)。我相信你在寻找一个递归函数。11>9>8>7>6这是一个SQL azure数据库。但是我现在看到的是条件typeid=1的根父级,因此父级应该是1和7,而不是1和6@DanielObrOlm忘了提一下,如果此表的大小合适,您需要在
parentid
列上建立索引。