I've read many post on the topic, but can't seem to quite get want I want.
Having the four tables: Product – Product_Version – Product_Version_Rule – Rule and creating a recordset accordingly...
(Notice the self-inheriting InheritsFromID. Enabling a so called Product Chain. In theory Product could inherit itself infinitely, i.e. making a never ending Product Chain, however IRL a Product inheriting-chain is no longer then three to five Product heritage.)
SELECT
Product.ProductID AS ID,
Product.InheritsFromID,
Product.Name AS ProductName,
Product_Version.Name AS VersionName,
-- Note, I have no real use for table *Product_Version_Rule*,
-- I'm just join-using it below to get to *Rule*(Name).
[Rule].Name AS RuleName,
[Rule].Value AS RuleValue
FROM
(
(
Product
FULL OUTER JOIN Product_Version
ON Product.ProductID = Product_Version.ProductID
)
FULL OUTER JOIN Product_Version_Rule
ON Product_Version.ProductVersionID
= Product_Version_Rule.ProductVersionID
)
LEFT JOIN [Rule]
ON Product_Version_Rule.RuleID = [Rule].RuleID
... resulting in...
row | ID InheritsFromID ProductName VersionName RuleName RuleValue | note
----+-----------------------------------------------------------------------+------
1 | 1 NULL ProdTemplateA (0) a 5 | *
2 | 1 NULL ProdTemplateA (0) b 15 | *
3 | 2 1 ProdComponentA (0) d 3 | **
4 | 2 1 ProdComponentA (0) c 11 | **
5 | 3 2 ProdEndA (0) s 1 | ***
6 | 3 2 ProdEndA (1) t hello | ***
7 | 4 NULL ProdTemplateB (0) a 3 | *
8 | 5 4 ProdEndB (1) c 21 | ***
* A "Start"-productcomponent since it does not inherits from a parent.
** A "Middle"-productcomponent since it both inherits from parent and has an inheriting child.
*** An "End"-productcomponent since no one inherits from it.
However I would like to get it presented hierarchically, preferably in XML. But finishing the query with just FOR XML AUTO, ELEMENTS
does not do the trick since I want a nested resultset, i.e:
<Product>
<ID>1</ID>
<InheritsFromID/>
<ProductName>ProdTemplateA</ProductName>
<Product_Version>
<VersionName>(0)</VersionName>
<Rule>
<RuleName>a</RuleName>
<RuleValue>5</RuleValue>
</Rule>
<Rule>
<RuleName>b</RuleName>
<RuleValue>15</RuleValue>
</Rule>
</Product_Version>
<Product>
<ID>2</ID>
<InheritsFromID>1</InheritsFromID>
<ProductName>ProdComponentA</ProductName>
<Product_Version>
<VersionName>(0)</VersionName>
<Rule>
<RuleName>d</RuleName>
<RuleValue>3</RuleValue>
</Rule>
<Rule>
<RuleName>c</RuleName>
<RuleValue>13</RuleValue>
</Rule>
</Product_Version>
<Product>
<ID>3</ID>
<InheritsFromID>2</InheritsFromID>
<ProductName>ProdEndA</ProductName>
<Product_Version>
<VersionName>(0)</VersionName>
<Rule>
<RuleName>s</RuleName>
<RuleValue>1</RuleValue>
</Rule>
</Product_Version>
<Product_Version>
<VersionName>(1)</VersionName>
<Rule>
<RuleName>t</RuleName>
<RuleValue>hello</RuleValue>
</Rule>
</Product_Version>
</Product>
</Product>
</Product>
<Product>
<ID>4</ID>
<InheritsFromID/>
<ProductName>ProdTemplateB</ProductName>
<Product_Version>
<VersionName>(0)</VersionName>
<Rule>
<RuleName>a</RuleName>
<RuleValue>3</RuleValue>
</Rule>
</Product_Version>
<Product>
<ID>5</ID>
<InheritsFromID>4</InheritsFromID>
<ProductName>ProdEndB</ProductName>
<Product_Version>
<VersionName>(1)</VersionName>
<Rule>
<RuleName>c</RuleName>
<RuleValue>21</RuleValue>
</Rule>
</Product_Version>
</Product>
</Product>
Any thought on how I can achieve above structure?
I'm thinking CTE but can't really get my head around it.