2

我正在尝试ClientID使用 group by 和 having 子句从以下子查询中提取,但出现以下错误:

Msg 116, Level 16, State 1, Line 1
当不使用 EXISTS 引入子查询时,选择列表中只能指定一个表达式。

询问:

select 
    ClientID 
from 
    SurveyResponses 
where 
     ClientID in (select ClientID, count (surveyresponseid) 
                  from SurveyResponses
                  where SurveyID in (1988,1989,2750,3206,15561) 
group by 
     ClientID
having count (SurveyResponseID) > 1) and SurveyID = 1989
4

5 回答 5

0

您在子查询中提取两列并且只能提取一列,因为您告诉 sql,检查调查响应计数中存在的客户 ID 以及 SurveyResponses 表中的客户 ID。

试试这个,它未经测试

select ClientID from SurveyResponses 
where ClientID in (select ClientID  from SurveyResponses
where SurveyID in (1988,1989,2750,3206,15561) 
group by ClientID
having count (SurveyResponseID) > 1) and SurveyID = 1989
于 2013-09-17T15:26:52.510 回答
0

你可以这样尝试:-

select ClientID from SurveyResponses where ClientID in
(select ClientID from SurveyResponses
where SurveyID in (1988,1989,2750,3206,15561) 
group by ClientID
having count (SurveyResponseID) > 1) and SurveyID = 1989
于 2013-09-17T15:27:22.213 回答
0
select 
    ClientID 
from 
    SurveyResponses 
where 
     ClientID in (select ClientID,  
                  from SurveyResponses
                  where SurveyID in (1988,1989,2750,3206,15561) 
group by 
     ClientID
having count (SurveyResponseID) > 1) and SurveyID = 1989

此子查询无效,因为您选择了多个字段ClientID, count (surveyresponseid) 。如果你想在subqueryforWhere In条件下处理多个字段,试试这个。处理多列

于 2013-09-17T15:29:35.697 回答
0

所有其他答案都可以正常工作,您也可以使用以下EXISTS语法:

SELECT clientID
FROM SurveyResponses
WHERE EXISTS (
         SELECT * 
         FROM SurveyResponses SR 
         WHERE SR.SurveyId IN (1988, 1989, 2759, 3206, 15561) 
               AND SR.ClientId = ClientID)
GROUP BY ClientID
HAVING COUNT(SurveyResponseID) > 1 AND SurveyID = 1989
于 2013-09-17T15:47:51.590 回答
-1

从 select 子句中删除count()

   select ClientID from SurveyResponses where ClientID in
    (select ClientID from SurveyResponses
    where SurveyID in (1988,1989,2750,3206,15561) 
    group by ClientID
    having count (SurveyResponseID) > 1) and SurveyID = 1989
于 2013-09-17T15:26:48.570 回答