2015-10-20 1077 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