我有一个工作正常的查询。
是否有可能如果table3的列(Child)仅与表1相关以在表1下而不是在表2下显示它,但同时另一个(Child)在表2中有父(通常是这种情况) 它将在表 2 下显示为当前所做的。
换句话说,子列直接位于表 2 的行列名称(父亲)之下,但有时它位于表 1 之下,与表 2 无关。
我怎样才能把它放在树视图的查询中?我假设我将不得不用 3 个 for 循环在 c# 中对输出进行编程,在第二个循环中我可以检查列是孙子还是子,并将其作为树视图的第二行或第二个节点,但我是在 sql 中构建查询时遇到问题。任何帮助深表感谢。
下面的查询显示了所有父母,然后是孩子,然后是孙子(一切都很好并且工作),但有时需要孩子代替父亲。
declare @x as xml
set @x =
(
SELECT distinct
Table1.AssetSysID, Table1.Asset_ID , Table1.FromLR, Table1.Asset_ID + ', ' + Table1.[Desc2] as GarndFather,
Table2.ACISysID ,Table2.PAssetSysID, Table2.FeatureName + ', ' + Table2.[DESC] AS Father,
Table3.ITMSysID ,Table3.Item_ID + ',' + Table3.[DESC] as Child
FROM Table1 left outer join
Table2 ON Table1.AssetSysID = Table2.PAssetSysID left outer join
Table3 ON Table1.AssetSysID = Table3.AssetSysID AND Table2.ACISysID = Table3.ACISysID
where (Table1.AssetType = @AssetType)
for xml auto,root('xml')
)
Asp.Net Telerik Treeview 数据绑定代码:
<DataBindings>
<telerik:RadTreeNodeBinding DataMember="Table1" TextField="Assets" ValueField="AssetSysID" ToolTip="Asset" ImageUrl="~/Images/DeleteIco.png"/>
<telerik:RadTreeNodeBinding DataMember="Table2" TextField="Feature" ValueField="ACISysID" ToolTip="Feature" ImageUrl="~/Images/CutIco.png"/>
<telerik:RadTreeNodeBinding DataMember="Table3" TextField="Equipment" ValueField="ITMSysID" ToolTip="Equipment" ImageUrl="~/Images/EditIco.png"/>
</DataBindings>
*最终代码:
select Table1.AssetObjID as "@AssetObjID",
Table1.Asset_ID as "@Asset_ID",
Table1.FromLR as "@FromLR",
Table1.AssetType + ', ' + Table1.StreetName + ', ' + Table1.FromMunicNo as "@FirstRow",
(
select Table2.ACIObjID as "@ACIObjID",
Table2.PAssetObjID as "@PAssetObjID",
Table2.Feature_ID + ', ' + Table2.FeatureName AS "@ChildOfFirstRow",
(
select Table3.ITMObjID as "@ITMObjID",
Table3.Item_ID + ',' + Table3.[DESC] as "@GrandChildOfFirstRow"
from Table3
where Table1.AssetObjID = Table3.AssetObjID and
Table2.ACIObjID = Table3.ACIObjID
for xml path('Table3'), type
)
from Table2
where Table1.AssetObjID = Table2.PAssetObjID
for xml path('Table2'), type
),
(
select Table3.ITMObjID as "@ITMObjID",
Table3.Item_ID + ',' + Table3.[DESC] as "@GrandChildOfFirstRow"
from Table3
where Table1.AssetObjID = Table3.AssetObjID and
Table2.ACIObjID <> Table3.ACIObjID
for xml path('Table3'), type
)
from Table1
where Table1.AssetType = 'xxxx'
for xml path('Table1'), root('xml')