好的,這是我的問題。
這裏的節點和邊桌的草圖:
[nodes]
node : varchar(xx)
[edges]
outputNode : varchar(xx)
inputNode : varchar(xx)
假設你的數據庫具有支持熱膨脹係數,然後構建這樣的查詢將彙集的關係和串聯的結果:
/* pair output nodes with a topic, assigned sequentially */
WITH OutputTopics(node, topicNumber) AS (
SELECT outputNode, ROW_NUMBER() (ORDER BY outputNode) AS topicNumber
FROM
(SELECT DISTINCT outputNode FROM edges) AS outputNodes
),
/* pair input nodes to the topic of associated output nodes */
InputTopicNumbers(inputNode, topicNumber) AS (
SELECT edges.inputNode, ot.topicNumber FROM edges INNER JOIN
OutputTopics AS ot ON ot.node=edges.outputNode
),
/* Recursive CTE to concat all topics together */
InputTopics(inputNode, topics, topicNumber) AS (
/* The seed for the recursion - all input nodes */
SELECT inputNode, CAST ('' AS nvarchar(max)), 0 /* max topic handled for node */
FROM InputTopicNumbers
GROUP BY inputNode
UNION ALL /* Add topics that are greater than those processed */
/* recursively concat topic numbers in ascending order */
SELECT i.inputNode, CONCAT(c.topics, ' --input-topic=T',i.topicNumber), i.topicNumber
FROM InputTopics AS c
INNER JOIN InputTopicNumbers i ON i.inputNode=c.inputNode
WHERE i.topicNumber > c.topicNumber
),
/* Bring it all together - append each node with '.exe',
list the output topic, if present
Use the recursive CTE to concat all inputTopics */
NodeCommands(node, exe, input, output) AS (
SELECT nodes.node,
CONCAT(nodes.node,'.exe'),
CONCAT(' --output_topic=T',ot.topicNumber), /* NULL if no output node */
it.topics
FROM nodes
LEFT OUTER JOIN OutputTopics AS ot ON ot.node=nodes.node
LEFT OUTER JOIN InputTopics AS it ON it.inputNode=nodes.node
)
/* finally our top-level query concatenates the parts to
arrive at a single command line */
SELECT CONCAT(
exe,
ISNULL(input, ''),
ISNULL(output, ''))
FROM NodeCommands ORDER BY node
我正在做這個蝙蝠,所以肯定有一些語法錯誤。我希望評論能夠解釋這個意圖。
您確定要按順序命名主題 - 這會不會令人困惑?如果主題名稱沒有意義,我會在發起過程後將其命名。這也會簡化你的查詢。 – mdma 2010-05-11 00:47:48
不,他們可以用任何可能的方式命名。當然,他們需要是獨一無二的。 – recipriversexclusion 2010-05-11 01:11:28