私はこのトピックに関する多くの投稿を読みましたが、私が望むものを完全に得ることができないようです.
Product – Product_Version – Product_Version_Rule – Ruleの 4 つのテーブルがあり、それに応じてレコードセットを作成しています...
(自己継承InheritsFromIDに注意してください。いわゆるProduct Chainを有効にします。理論的には、 Productは無限に継承できます。つまり、終わりのないProduct Chainを作成できますが、IRL a Product inheriting-chain は、もはや 3 ~ 5 個の Product 遺産ではありません。)
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
... その結果...
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 | ***
* 親から継承しないため、"Start"-productcomponent です。
** 親から継承し、継承する子を持つため、「中間」製品コンポーネント。
*** 誰も継承しないため、「最終」製品コンポーネント。
ただし、できればXMLで階層的に表示したいと思います。FOR XML AUTO, ELEMENTS
しかし、ネストされた結果セットが必要なため、次のようにクエリを終了してもうまくいきません。
<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>
上記の構造をどのように達成できるかについて何か考えはありますか?
私はCTEを考えていますが、実際には頭に浮かびません。