SQL从2个不同的表中划分ID的数量?

时间:2018-11-24 15:55:38

标签: sql

我有以下问题,我有2张桌子:

Surveys (surveyID, topic, text)
Response (responseID, response_text, response_value)

我的问题如下:如何获得每个调查的平均答复数?

假设我有2个SurveyID和8个AnswerID,如何将8个responseID除以2个surveyID

我的想法是,但这没有解决:

SELECT (COUNT(surveyID) / COUNT(responseID)) 
FROM Surveys, Response;

3 个答案:

答案 0 :(得分:0)

您可以尝试使用sql-serevr:

with x as(
Select count(*) as totSurveys, 0 as TotResponse
from Surveys
union
Select 0 as totSurveys, count(*) as TotResponse
from Response
) select TotResponse/totSurveysas Result
from x

或者您可以:

select (Select count(*) from Surveys)/(Select count(*) from Response)

答案 1 :(得分:0)

要明确说明您的问题:

SELECT (COUNT(distinct surveyID) / COUNT(distinct responseID)) 
FROM Surveys, Response;

(不同的指令指示DBMS仅计算结果中的唯一值)

答案 2 :(得分:0)

  SELECT (COUNT(distinct surveyID) / COUNT(distinct 
   responseID)) 
   FROM Surveys, Response
  

唯一的原因是               意外的输出是单独的重复项                 您的                表使用此功能,您将获得所需的