2013-03-17 43 views
0

我對家電的家庭擁有的數據,每列的家電,和數據Y或N的格式我想生成與每戶擁有家電和新列。當我運行以下腳本(SQLIte)時,出現「(」)附近的語法錯誤錯誤消息。請幫助 - 我嘗試了各種語法。如何在行內添加符合條件的列的個案?

SELECT Household, 
SUM((CASE WHEN Stove="Y" THEN 1 ELSE 0) + 
(CASE WHEN Fridge="Y" THEN 1 ELSE 0) + 
(CASE WHEN TV="Y" THEN 1 ELSE 0) + 
(CASE WHEN Video="Y" THEN 1 ELSE 0) + 
(CASE WHEN SatDish="Y" THEN 1 ELSE 0) + 
(CASE WHEN Radio="Y" THEN 1 ELSE 0) + 
(CASE WHEN FixPhone="Y" THEN 1 ELSE 0) END) 
AS Appliances 
FROM Assets 
+0

你試過下面的查詢? – 2013-03-17 16:31:28

回答

1

你需要與每一個case語句結尾: 試試這個:

SELECT Household, 
SUM((CASE WHEN Stove="Y" THEN 1 ELSE 0 End) + 
(CASE WHEN Fridge="Y" THEN 1 ELSE 0 End) + 
(CASE WHEN TV="Y" THEN 1 ELSE 0 End) + 
(CASE WHEN Video="Y" THEN 1 ELSE 0 End) + 
(CASE WHEN SatDish="Y" THEN 1 ELSE 0 End) + 
(CASE WHEN Radio="Y" THEN 1 ELSE 0 End) + 
(CASE WHEN FixPhone="Y" THEN 1 ELSE 0 End)) 
AS Appliances 
FROM Assets 
Group By Household 
+0

它的工作。感謝一堆! – Wayne 2013-03-17 16:41:06

+0

如果它適合你,你能標記爲答案嗎? – 2013-03-17 16:53:25

相關問題