我想考第一這個問題的答案:需要DB2查詢轉換爲TSQL
SQL - message schema - need to find an existing message thread given a set of users
首先回答這個問題寫在DB2,我有一個困難時期將答案轉換爲TSQL。有人能幫我解決這個問題嗎?這裏的查詢:
WITH Selected_Users(id) as (VALUES (@id1), (@id2), --etc--),
Threads(id) as (SELECT DISTINCT threadFk
FROM ThreadMembers as a
JOIN Selected_Users as b
ON b.id = a.userFk)
SELECT a.id
FROM Threads as a
WHERE NOT EXISTS (SELECT '1'
FROM ThreadMembers as b
LEFT JOIN Selected_Users as c
ON c.id = b.userFk
WHERE c.id IS NULL
AND b.threadFk = a.id)
AND NOT EXISTS (SELECT '1'
FROM Selected_Users as b
LEFT JOIN ThreadMembers as c
ON c.userFk = b.id
AND c.threadFk = a.id
WHERE c.userFk IS NULL)
查詢的描述是答案,這有很大幫助的一部分。查詢的第一部分創建一個名爲Selected_Users的臨時表,但我不確定這將如何完成。提前致謝!
我更新了TSQL版本的原始問題。謝謝! – Redtopia 2012-03-28 23:53:49