2013-04-28 11 views
6

我正在處理具有層次結構的資產數據庫。此外,還有一個「ReferenceAsset」表,它有效地指向資產。參考資產基本上起着重寫的作用,但它被選爲好像它是一種獨特的新資產。其中一個被設置的覆蓋是parent_id。SQL Server:查詢分層和引用數據

列,其相關的選擇層次結構:
資產:ID(主),PARENT_ID
資產參考:ID(主),ASSET_ID(foreignkey->資產),PARENT_ID(總是一個資產)
- --EDITED 5/27 ----

樣品培訓相關表數據(加入後):

id | asset_id | name   | parent_id | milestone | type 

    3  3  suit    null  march  shape 
    4  4  suit_banker   3   april  texture 
    5  5  tie    null  march  shape 
    6  6  tie_red    5   march  texture 
    7  7  tie_diamond   5   june  texture 
    -5  6  tie_red    4   march  texture 

的0(如最後一行)表示被引用的資產。被引用的資產有幾個過期的列(在這種情況下,只有parent_id很重要)。

的期望是,如果我從四月中選擇所有資產,我應該做的第二選擇,以獲得匹配查詢的全部樹枝:

所以最初查詢匹配會導致:

4  4  suit_banker   3   april  texture 

然後CTE後,我們得到了完整的層次,我們的結果應該是這樣的(到目前爲止,這是工作)

3  3  suit    null  march  shape 
    4  4  suit_banker   3   april  texture 
    -5  6  tie_red    4   march  texture 

,你會看到,ID的父:-5是存在的,但缺少什麼,那是需要的,是被引用的資產,並且引用的資產的母公司:

5  5  tie    null  march  shape 
    6  6  tie_red    5   march  texture 

目前我的解決方案適用於此,但它僅限於單一深度的引用(並且我覺得實現非常難看)。

---編輯---- 這是我的主要選擇功能。這應該更好地證明真正的複雜性所在:AssetReference。

Select A.id as id, A.id as asset_id, A.name,A.parent_id as parent_id, A.subPath, T.name as typeName, A2.name as parent_name, B.name as batchName, 
L.name as locationName,AO.owner_name as ownerName, T.id as typeID, 
M.name as milestoneName, A.deleted as bDeleted, 0 as reference, W.phase_name, W.status_name 
FROM Asset as A Inner Join Type as T on A.type_id = T.id 
Inner Join Batch as B on A.batch_id = B.id 
Left Join Location L on A.location_id = L.id 
Left Join Asset A2 on A.parent_id = A2.id 
Left Join AssetOwner AO on A.owner_id = AO.owner_id 
Left Join Milestone M on A.milestone_id = M.milestone_id 
Left Join Workflow as W on W.asset_id = A.id 
where A.deleted <= @showDeleted 

UNION 

Select -1*AR.id as id, AR.asset_id as asset_id, A.name, AR.parent_id as parent_id, A.subPath, T.name as typeName, A2.name as parent_name, B.name as batchName, 
L.name as locationName,AO.owner_name as ownerName, T.id as typeID, 
M.name as milestoneName, A.deleted as bDeleted, 1 as reference, NULL as phase_name, NULL as status_name 
FROM Asset as A Inner Join Type as T on A.type_id = T.id 
Inner Join Batch as B on A.batch_id = B.id 
Left Join Location L on A.location_id = L.id 
Left Join Asset A2 on AR.parent_id = A2.id 
Left Join AssetOwner AO on A.owner_id = AO.owner_id 
Left Join Milestone M on A.milestone_id = M.milestone_id 
Inner Join AssetReference AR on AR.asset_id = A.id 
where A.deleted <= @showDeleted 

我有一個存儲過程,需要一個臨時表(#temp)並查找層次結構的所有元素。我所採用的策略是這樣的:

  1. 選擇整個系統層次結構到一個臨時表用逗號分隔的每個整個樹枝列表來表示(#treeIDs)
  2. 獲取匹配查詢資產整體的層次結構(從#臨時)
  3. 獲取所有參考資產所指向的資產從層次結構
  4. 解析所有參考資產

這適用於現在的層次結構,因爲參考的資產永遠是拉如果他們不是,我想我會陷入麻煩。我覺得我需要一些更好的遞歸形式。

這裏是我當前的代碼,這是工作,但我不感到自豪,我知道這是不穩健(因爲它只有在引用底部作品):

第1步。構建整個層次

;WITH Recursive_CTE AS (
SELECT Cast(id as varchar(100)) as Hierarchy, parent_id, id 
FROM #assetIDs 
Where parent_id is Null 

UNION ALL 

SELECT 
CAST(parent.Hierarchy + ',' + CAST(t.id as varchar(100)) as varchar(100)) as Hierarchy, t.parent_id, t.id 
FROM Recursive_CTE parent 
INNER JOIN #assetIDs t ON t.parent_id = parent.id 
) 



Select Distinct h.id, Hierarchy as idList into #treeIDs 
FROM (Select Hierarchy, id FROM Recursive_CTE) parent 
CROSS APPLY dbo.SplitIDs(Hierarchy) as h 

步驟2.選擇匹配查詢

Select DISTINCT L.id into #RelativeIDs FROM #treeIDs 
CROSS APPLY dbo.SplitIDs(idList) as L 
WHERE #treeIDs.id in (Select id FROM #temp) 

步驟3.所有資產的分支獲取所有參考資產在枝頭 (參考資產有負的ID值,因此ID < 0部分)

Select asset_id INTO #REFLinks FROM #AllAssets WHERE id in 
(Select #AllAssets.asset_id FROM #AllAssets Inner Join #RelativeIDs 
on #AllAssets.id = #RelativeIDs.id Where #RelativeIDs.id < 0) 

第4步:獲取的任何分支在步驟3

Select DISTINCT L.id into #extraRelativeIDs FROM #treeIDs 
CROSS APPLY dbo.SplitIDs(idList) as L 
WHERE 
exists (Select #REFLinks.asset_id FROM #REFLinks WHERE #REFLinks.asset_id = #treeIDs.id) 
and Not Exists (select id FROM #RelativeIDs Where id = #treeIDs.id) 

我一直在努力,只是顯示找到相關的代碼。我非常感謝任何能夠幫助我找到更好解決方案的人!

+0

你使用的是什麼sql版本? http://msdn.microsoft.com/de-de/library/bb677290.aspx – NickD 2013-04-28 07:48:54

+0

sql server 2012,但我們只是切換到它,所以這大部分是寫於2008 – haggercody 2013-04-28 07:58:29

回答

1
--getting all of the children of a root node (could be > 1) and it would require revising the query a bit 

DECLARE @AssetID int = (select AssetId from Asset where AssetID is null); 


--algorithm is relational recursion 
--gets the top level in hierarchy we want. The hierarchy column 
--will show the row's place in the hierarchy from this query only 
--not in the overall reality of the row's place in the table 

WITH Hierarchy(Asset_ID, AssetID, Levelcode, Asset_hierarchy) 
AS 
(
SELECT AssetID, Asset_ID, 
     1 as levelcode, CAST(Assetid as varchar(max)) as Asset_hierarchy 
FROM Asset 
WHERE [email protected] 

UNION ALL 

--joins back to the CTE to recursively retrieve the rows 
--note that treelevel is incremented on each iteration 

SELECT A.Parent_ID, B.AssetID, 
     Levelcode + 1 as LevelCode, 
     A.assetID + '\' + cast(A.Asset_id as varchar(20)) as Asset_Hierarchy 
FROM Asset AS a 
      INNER JOIN dbo.Batch AS Hierarchy 
      --use to get children, since the parentId of the child will be set the value 
      --of the current row 
      on a.assetId= b.assetID 
--use to get parents, since the parent of the Asset_Hierarchy row will be the asset, 
      --not the parent. 
      on Asset.AssetId= Asset_Hierarchy.parentID 


SELECT a.Assetid,a.name, 
     Asset_Hierarchy.LevelCode, Asset_Hierarchy.hierarchy 
FROM  Asset AS a 
     INNER JOIN Asset_Hierarchy 
       ON A.AssetID= Asset_Hierarchy.AssetID 
ORDER BY Hierarchy ; 
--return results from the CTE, joining to the Asset data to get the asset name 
---that is the structure you will want. I would need a little more clarification of your table structure 
+0

哇。輝煌。謝謝! – haggercody 2014-06-03 05:56:12

0

這將有助於瞭解您的基礎表結構。有兩種方法應該根據您的環境而工作:SQL瞭解XML,因此您可以將SQL作爲xml結構,或者只包含一個表,每個行項具有唯一的主鍵ID和一個parentid。 id是parentid的fk。節點的數據只是標準列。您可以使用cte或函數爲計算列提供動力,以確定每個節點的嵌套程度。限制是一個節點只能有一個父節點。

+0

感謝您看看這個。
我已經用選擇函數更新了OP,它更好地顯示了表結構。這個問題真的在AssetReference表中。讓我解釋一個特定的情況: 考慮我選擇資產的批量帽子: 我得到[BallCap] - 然後我選擇[BallCap]的整個樹分支(迄今爲止) 但是然後我遇到一個AssetReference in樹 現在我必須選擇原始資產引用 然後我需要整個樹爲這個資產 – haggercody 2013-05-01 03:26:59

+0

你是說你有一個節點的分支,並在其中一個分支可以是一個節點上一個完全不同的樹? – 2013-05-01 07:20:21

+0

是的,分支中的節點可以'引用'另一個可能位於不同樹分支中的節點。此「參考」來自不同的表格(AssetReference)。我在這裏違反了設計規定嗎? – haggercody 2013-05-05 07:30:10