Reputation: 3057
I have a query which is working fine.
Is it possible that if the table3's column(Child) is only related to table 1 to show it under table 1 and not under table 2, but at the same time another (Child) has a parent in table 2 (which usually is the case) it will show under table 2 as its currently doing.
In other words Child column is directly under Table2's row column name (Father), but occasionally it comes under Table1 with no relation to Table 2.
How can I out put that in a query for a treeview? I am assuming that I will have to program the out come in c# also with 3 for loops and in the second loop I can check if the column is grandchild or Child and make that as a second row or 2nd node of treeview, but I am having a problem building a query in sql. Any help is much appreciated.
The query below shows all Parent, then child then grand child(all well and working), but what is desired is at times child takes place of a father.
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 DataBinding Code:
<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>
*Final Code:
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')
Upvotes: 0
Views: 315
Reputation: 3057
Got an Idea from Mikael Eriksson and help from team lead @ work
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')
So It goes something like this:
<Table1 atrributes......... FirstRow=".........."/>
<Table2 atrributes......... ChildOfFirstRow="..........">
<Table3 atrributes......... GrandChildOfFirstRow=".........."/>
</Table2>
<Table3 atrributes......... GrandChildOfFirstRow="This GrandChild took the importance as equal to ChildOfFirstRow"/>
Upvotes: 1