我試圖創建一個查詢,將產生約40的自定義列的交叉檢驗表格,顯示Y或N.現在我有SQL Server查詢 - 循環問題
SELECT DISTINCT [Company],
[Option1],
[Option2],
[Option3],
CASE
WHEN [Table1].[ID1] IN (SELECT ID2 FROM Table2 WHERE Variable = 1 AND Bit = 1) THEN
'Y'
ELSE 'N'
END AS 'CustomColumn1:',
CASE
WHEN [Table1].[ID1] IN (SELECT ID2 FROM Table2 WHERE Variable = 2 AND Bit = 1) THEN
'Y'
ELSE 'N'
END AS 'CustomColumn1:',
CASE
WHEN [Table1].[ID1] IN (SELECT ID2 FROM Table2 WHERE Variable = 3 AND Bit = 1) THEN
'Y'
ELSE 'N'
END AS 'CustomColumn1:',
.............
-- REPEAT ANOTHER 40 times
FROM [Table1]
WHERE [Table1].[OtherCondition] = 'True'
ORDER BY [Company]
所以我的問題是,我該如何創建一個循環(while?for?),它將循環變量,並根據條件爲行分配Y或N,而不是創建40個Case語句?
我假設您的示例中存在拼寫錯誤,並且CustomColumn應該用變量遞增(CustomColumn1,CustomColumn2 ...),而不是總是「CustomColumn1」。 – 2010-08-05 20:54:08
感謝Joe,您是對的,CustomColumn名稱確實會更改,並非總是CustomColumn1。 – wes 2010-08-05 21:10:55