2013-04-28 50 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