簡體   English   中英

在同一個表中加入以檢索層次結構(父子層次結構)

[英]Join within same table to retrieve hierarchy (Parent-Child Hierarchy)

我有兩張桌子 - MasterChild 子表包含存儲在主表中的 MainId 的分層數據。

Master.Name 是唯一的

Master.MainId = Child.ChildId

Child.ParentId = Child.ChildId

我必須從子表中檢索 Master.RelDate 和 Master.Name 的所有數據。 我認為這是一個直接的查詢,但不知何故無法提取所需的 output。 無論我如何編寫查詢,我最終都會得到一個子表記錄。

它與表結構或我的查詢有關嗎?

注意:ParentIds 和 ChildIds 都不是按順序排列的。 該數據來自另一個系統,為了簡單起見,我只是將這些值放入。

IF OBJECT_ID('tempdb..#Master') IS NOT NULL BEGIN DROP TABLE #Master END
GO
IF OBJECT_ID('tempdb..#Child') IS NOT NULL BEGIN DROP TABLE #Child END
GO
CREATE TABLE #Master
(
    Id BIGINT IDENTITY(1,1),
    RelDate DATE NOT NULL,
    MainId BIGINT NOT NULL,
    Name VARCHAR(512) NOT NULL
)
GO
CREATE TABLE #Child
(
    Id BIGINT IDENTITY(1,1),
    RelDate DATE NOT NULL,
    ChildId BIGINT NOT NULL,
    ParentId BIGINT NULL,
    Label VARCHAR(20) NULL,
    Value VARCHAR(1024) NULL
)
GO

INSERT INTO #Master(RelDate,MainId,Name) VALUES ('2019-01-01',1,'Name1')
INSERT INTO #Master(RelDate,MainId,Name) VALUES ('2019-01-01',11,'Name11')
GO
INSERT INTO #Child(RelDate,ChildId,ParentId,Label,Value) VALUES
('2019-01-01',1,2,'Level10',NULL)
,('2019-01-01',2,3,'Level09',NULL)
,('2019-01-01',3,4,'Level08',NULL)
,('2019-01-01',4,5,'Level07',NULL)
,('2019-01-01',5,6,'Level06','This is level 6')
,('2019-01-01',6,7,'Level05',NULL)
,('2019-01-01',7,8,'Level04',NULL)
,('2019-01-01',8,9,'Level03','This is level 3')
,('2019-01-01',9,10,'Level02',NULL)
,('2019-01-01',10,11,'Level01',NULL) -- Always same
,('2019-01-01',11,NULL,'Root',NULL) -- Always same

INSERT INTO #Child(RelDate,ChildId,ParentId,Label,Value) VALUES
('2019-01-01',11,12,'Level10',NULL)
,('2019-01-01',12,13,'Level09',NULL)
,('2019-01-01',13,14,'Level08',NULL)
,('2019-01-01',14,15,'Level07',NULL)
,('2019-01-01',15,16,'Level06','This is level 6')
,('2019-01-01',16,17,'Level05',NULL)
,('2019-01-01',17,18,'Level04',NULL)
,('2019-01-01',18,19,'Level03','This is level 3')
,('2019-01-01',19,10,'Level02',NULL)
,('2019-01-01',10,11,'Level01',NULL) -- Always same
,('2019-01-01',11,NULL,'Root',NULL) -- Always same
GO

SELECT * FROM #Master
SELECT * FROM #Child

RelDate = 2019-01-01 和 Name = Name1 的所需 output

SELECT chld.*
FROM #Master mst (NOLOCK)
    INNER JOIN #Child chld (NOLOCK) ON (mst.MainId = chld.ChildId)
    LEFT JOIN #Child chld1 (NOLOCK) ON (chld.ParentId = chld.ChildId)
WHERE mst.RelDate = '2019-01-01' -- Input 1
AND mst.Name = 'Name1' -- Input 2

Child 表中的所有數據

Id  RelDate ChildId ParentId    Label   Value
1   2019-01-01  1   2   Level10 NULL
2   2019-01-01  2   3   Level09 NULL
3   2019-01-01  3   4   Level08 NULL
4   2019-01-01  4   5   Level07 NULL
5   2019-01-01  5   6   Level06 This is level 6
6   2019-01-01  6   7   Level05 NULL
7   2019-01-01  7   8   Level04 NULL
8   2019-01-01  8   9   Level03 This is level 3
9   2019-01-01  9   10  Level02 NULL
10  2019-01-01  10  11  Level01 NULL
11  2019-01-01  11  NULL    Root    NULL

提前致謝

select c.* from #Child c inner join #Master M
on M.MainId = C.ChildId

Id  RelDate ChildId ParentId    Label   Value
1   2019-01-01  1   2   Level10 NULL
11  2019-01-01  11  NULL    Root    NULL

如果這不是所需的 output,請提供您所需的 output。

它接縫表中的數據有點不合邏輯。 尤其是“Name11”。 我假設每個新行都會從 Childid 列中丟失一個 id。現在我們在第 19 行丟失了“lvl2”。
我試圖找出一些解決方案,現在我的 position 在這里。
它適用於“Name1”,但“Name11”的結果有點不同。

SELECT   t2.* 
FROM #Master mst 
      join  #Child t1 ON mst.MainId = t1.ChildId
           join  #Child t2 ON t2.parentid >= t1.ChildId  

WHERE mst.RelDate = '2019-01-01' -- Input 1
AND mst.Name = 'Name1'           -- Input 2

order by t2.id 
offset 0 rows 
fetch first 10 rows only

也許它對你有一點幫助。 我會嘗試找到一個棘手的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM