2016-05-20 3 views
-1
ItemId  Name   parentId 
1   A    null 
2   b    null 
3   c     1 
4   d     2 
5   e     3   
6   f     4 
7   g     2 

こんにちは私はSQLクエリを作成するのに助けが必要です。 itemid、name、parentitemidの3つのカラムを含む表があります。私はSQLクエリを必要とする親の子relation.if parentitemidのIDが必要な場合は、ルートを意味します。あなたが使用することができる。例えばは、SQLクエリを作成する際に助けが必要です

<1><3><5></5> </3></1> 
+0

1 Aヌル、2 Bヌル、3 C 1,4 D 2,5 E 3,6 F 4、7 G 2行のデータ –

+1

何を試しましたか?どういうことですか? –

+0

私は親子関係の再帰的なクエリを作成する方法を知っていませんが、もしあなたがこれを返信してくれば返信してください。 –

答えて

1

WITH HierarchicalTable 
AS 
(
    SELECT Id, ParentId, Name, 0 as [Level] 
     FROM YourTable 
     WHERE ParentId IS NULL 
    UNION ALL 
    SELECT YourTable.Id, YourTable.ParentId, YourTable.Name, [Level] + 1 
     FROM YourTable 
     JOIN HierarchicalTable ON HierarchicalTable.Id = YourTable.ParentId 
) 
SELECT [Level], Name FROM HierarchicalTable 
+1

私は

0

これは、過度に複雑なソリューションですが、それはあなたの問題のために動作します:

DECLARE @temp TABLE (ItemId int, Name char(1), parentId int,l int) 
DECLARE @xml TABLE (s nvarchar(max), e nvarchar(max), parentId int, itemid int) 
DECLARE @l int 
;WITH cte AS (
SELECT * 
FROM (VALUES 
(1, 'a', NULL),(2, 'b', NULL),(3, 'c', 1),(4, 'd', 2),(5, 'e', 3),(6, 'f', 4),(7, 'g', 2) 
) as t(ItemId, Name, parentId) 
--Here we create recursive cte to obtain levels of nesting 
), res AS (
    SELECT *, 
      1 [Level] 
    FROM cte c 
    where parentId IS null 
    UNION ALL 
    SELECT c.*, 
      [Level]+1 
    FROM res r 
    INNER JOIN cte c 
    ON c.parentId = r.ItemId 
) 
--put results into temp table 
INSERT INTO @temp 
SELECT * 
FROM res 
--obtain max level 
SELECT @l = MAX(l) 
FROM @temp 
--from max level to 1 begin 
WHILE @l > 0 
BEGIN 
    --if there is nodes with same parentid - concatinating them 
    UPDATE x 
    SET x.e = x.e + v.s + v.e 
    FROM @xml x 
    INNER JOIN @xml v 
     ON v.parentId = x.parentId and v.e !=x.e; 

    --here we merge table with results 
    -- first run <e></e> 
    -- next run <c><e></e></c> 
    -- next run <a><c><e></e></c></a> 
    MERGE @xml AS target 
    USING (
     SELECT '<'+ Name +'>' as s,'</'+ Name + '>' as e, parentId, ItemId 
     FROM @temp 
     WHERE l = @l 
     ) as source 
    ON target.parentid = source.itemid 
    WHEN NOT MATCHED THEN INSERT VALUES (source.s, source.e, source.parentId, source.ItemId) 
    WHEN MATCHED THEN 
     UPDATE 
      SET target.s = source.s + target.s, 
       target.e = target.e + source.e, 
       target.parentid = source.parentid, 
       target.itemid = source.itemid; 
    --next level down 
    SET @l = @l - 1 

END 

SELECT x --CAST(x as xml) 
FROM (
    SELECT s+e as x, 
      DENSE_RANK() OVER (PARTITION BY itemid ORDER BY s ASC) as rn 
      --need that column to obtain first one of every string for itemid 
    FROM @xml 
    ) as c 
WHERE c.rn = 1 
--FOR XML PATH ('') 

出力は次のようになります。

x 
<a><c><e></e></c></a> 
<b><d><f></f></d><g></g></b> 

を削除した場合​​近く、最後のクエリでこのSELECT CAST(x as xml)このSELECT x --CAST(x as xml)を変更するには、これを取得する:

<a> 
    <c> 
    <e /> 
    </c> 
</a> 
<b> 
    <d> 
    <f /> 
    </d> 
    <g /> 
</b> 
+0

おかげgofr非常に役立つようなXMLが必要ですが、また、ノード –

+0

とアイテムIDとのParentID必要あなたの質問 - '<1><3><5> 'コメントで今' 'に答えるために - 何かを。正確に何が必要ですか? – gofr1

関連する問題