2015-10-20 100 views
0

美好的一天,SQL SELECT查詢排除值

我想選擇與平均領域

SELECT 
    CAST(SurveyDept AS NVARCHAR(100)) as Dept_name, 
    CAST(SurveySubDept AS NVARCHAR(100)) as Subdept_name 
    AVG(SurveyReachability) as total_R, 
    AVG(SurveyProfessionalism) as total_P, 
    AVG(SurveyProactiveness) as total_Pr, 
    AVG(SurveyCompetence) as total_C, 
    AVG(SurveyResponse) as total_Re 
FROM 
    dbo.tb_SurveyDeptInfo 
GROUP BY 
    CAST(SurveyDept AS NVARCHAR(100)), 
    CAST(SurveySubDept AS NVARCHAR(100)) 
ORDER BY 
    CAST(SurveyDept AS NVARCHAR(100)), 
    CAST(SurveySubDept AS NVARCHAR(100)) 

結果看起來像這樣的一個表:

enter image description here

什麼我想要做的是排除具有值(轉移)和值(檢查&公用事業服務付款)的「子部門名稱」(如果存在)

所以我加了(按順序)後,下方:

WHERE Subdept_name NOT IN (SELECT * 
          FROM dbo.tb_SurveyDeptInfo 
          WHERE CAST(SurveySubDept AS NVARCHAR(100)) = 'Transfers' 
           OR CAST(SurveySubDept AS NVARCHAR(100)) = 'Check & Utility Service Payments') 

你能勸哪裏出了問題,它不返回任何記錄

+0

是有可能在下添加一個新的列(調查響應),此列將包含整個行本身的AVG? – user1000744

回答

2

只要你WHERE子句添加到整個查詢:

SELECT CAST(SurveyDept AS NVARCHAR(100)) as Dept_name, 
     CAST(SurveySubDept AS NVARCHAR(100)) as Subdept_name 
     AVG(SurveyReachability) as total_R, 
     AVG(SurveyProfessionalism) as total_P, 
     AVG(SurveyProactiveness) as total_Pr, 
     AVG(SurveyCompetence) as total_C, 
     AVG(SurveyResponse) as total_Re 
FROM dbo.tb_SurveyDeptInfo 
WHERE CAST(SurveySubDept AS NVARCHAR(100)) NOT IN ('Transfers', 'Check & Utility Service Payments') 
GROUP BY CAST(SurveyDept AS NVARCHAR(100)), CAST(SurveySubDept AS NVARCHAR(100)) 
ORDER BY CAST(SurveyDept AS NVARCHAR(100)),CAST(SurveySubDept AS NVARCHAR(100)); 

由於您有IN (SELECT * . .),因此您的查詢不起作用。子查詢返回多個列,因此會出現錯誤。

我不明白你爲什麼要對NVARCHAR(100)做明確的情況。這些似乎只是混淆了查詢。我會嘗試:

SELECT SurveyDept as Dept_name, SurveySubDept as Subdept_name 
     AVG(SurveyReachability) as total_R, 
     AVG(SurveyProfessionalism) as total_P, 
     AVG(SurveyProactiveness) as total_Pr, 
     AVG(SurveyCompetence) as total_C, 
     AVG(SurveyResponse) as total_Re 
FROM dbo.tb_SurveyDeptInfo 
WHERE SurveySubDept NOT IN ('Transfers', 'Check & Utility Service Payments') 
GROUP BY SurveyDept, SurveySubDept 
ORDER BY SurveyDept, SurveySubDept; 
+0

它說[微軟] [ODBC SQL Server驅動程序] [SQL Server]兩個答案附近'AVG'的語法不正確 – user1000744

+0

好吧,現在它的工作原理,我放在前面集團的地方,謝謝。 – user1000744

0

試試這個

select * from (
SELECT CAST(SurveyDept AS NVARCHAR(100)) as Dept_name, 
CAST(SurveySubDept AS NVARCHAR(100)) as Subdept_name 
AVG(SurveyReachability) as total_R, 
AVG(SurveyProfessionalism) as total_P, 
AVG(SurveyProactiveness) as total_Pr, 
AVG(SurveyCompetence) as total_C, 
AVG(SurveyResponse) as total_Re 
FROM dbo.tb_SurveyDeptInfo GROUP BY CAST(SurveyDept AS NVARCHAR(100)), CAST(SurveySubDept AS NVARCHAR(100)) 
ORDER BY CAST(SurveyDept AS NVARCHAR(100)),CAST(SurveySubDept AS NVARCHAR(100)) 
) tmp 
WHERE tmp.Subdept_name NOT IN (
SELECT * 
FROM dbo.tb_SurveyDeptInfo 
WHERE 
CAST(SurveySubDept AS NVARCHAR(100)) = 'Transfers' 
OR 
CAST(SurveySubDept AS NVARCHAR(100)) = 'Check & Utility Service Payments' 
) 
+0

它說[微軟] [ODBC SQL Server驅動程序] [SQL Server]'AVG' – user1000744

+0

附近的語法不正確可以再次運行您的查詢內部查詢,看看您是否獲取數據? – Utsav

+0

好吧,現在它的工作原理,我把前面的小組放在哪裏,謝謝。 – user1000744