並び替えで悩んでいます。
次のようなテーブルがあります。
aspect_id (int)
aspect_text (memo)
root_id (int) which has as a foreign key a aspect_id
次のダミーデータを持つ非循環ツリーがあります。
aspect_id aspect_text root_id
1 root null
2 aspect1 1
3 aspect2 1
4 aspect3 2
5 aspect5 4
この例では、データは正しくソートされていますが、私のデータベースではそうではありません。ルート要素から開始して子を見つけ、その子を出力して再帰的に実行するように並べ替えたいと思います。
CTEを使用すると、かなり実行可能です。アクセスはこれをサポートしていません。CTE を使用すると、次のようになります。
WITH aspectTree (aspect_id, root_id, Level#) AS
(
Select
aspect.aspect_id,
aspect.root_id,
0
FROM aspect
WHERE aspect.aspect_id = 44
UNION ALL
SELECT
aspect.aspect_id,
aspect.root_id,
T.Level# + 1
FROM aspect
INNER JOIN aspectTree AS T
On T.aspect_id = aspect.root_id
)
SELECT * FROM aspectTree;