小编San*_*rus的帖子

将两个小查询(按不同值组分组)合并为一个查询

请查看下表(称为响应).它显示了受访者对问题和答案的回应.

questionid  answerid    respondentid
       1          10        1
       1          11        2
       1          11        4
       1          12        3
       1          12        5
       2          20        1
       2          20        2
       2          21        2
       2          22        1
       2          22        4
       2          23        1
       2          23        3
       2          24        4
       3          30        2
       3          30        3
       3          30        4
       3          31        1
Run Code Online (Sandbox Code Playgroud)

我们可以运行以下SQL:

select questionid, answerid, count(respondentid) as noOfRespondentsToQuestionAndAnswer
from response
group by questionid, answerid
Run Code Online (Sandbox Code Playgroud)

...这将告诉我们有多少受访者回答了问题+答案的每个组合.

我们也可以这样做:

select questionid, count(distinct respondentid) as noOfRespondentsToQuestion
from response
group by …
Run Code Online (Sandbox Code Playgroud)

sql

3
推荐指数
1
解决办法
9322
查看次数

标签 统计

sql ×1