SQLZOO:8+ Numeric Examples

这篇博客通过SQLZOO的示例介绍了如何解决涉及数值的查询问题,包括计算百分比、查找特定条件下的得分、按主题汇总响应人数等。内容涵盖了计算机科学和创意艺术与设计等学科,并涉及了数据处理和百分比计算的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.The example shows the number who responded for:

question 1
at ‘Edinburgh Napier University’
studying ‘(8) Computer Science’
Show the the percentage who STRONGLY AGREE

SELECT round(A_STRONGLY_AGREE/
(A_STRONGLY_DISAGREE+
A_DISAGREE+
A_NEUTRAL+
A_AGREE+
A_STRONGLY_AGREE)*100,0)
FROM nss
WHERE question='Q01'
AND institution='Edinburgh Napier University'
AND subject='(8) Computer Science'
  • 用到之前有一章里用过的取百分比的方法。

2.Show the institution and subject where the score is at least 100 for question 15.

SELECT institution,subject
  FROM nss
 WHERE question='Q15'
   AND score>=100

3.Show the institution and score where the score for ‘(8) Computer Science’ is less than 50 for question ‘Q15’

SELECT institution,score
  FROM nss
 WHERE question='Q15'
   AND score<50
   AND subject='(8) Computer Science'
  • 和牛客的比起来sqlzoo的题真的简单好多。在牛客被打击了就来sqlzoo找找自信吧

4.Show the subject and total number of students who responded to question 22 for each of the subjects ‘(8) Computer Science’ and ‘(H) Creative Arts and Design’.
HINT
You will need to use SUM over the response column and GROUP BY subject

SELECT subject,sum(response)
  FROM nss
 WHERE question='Q22'
   AND subject in ('(8) Computer Science','(H) Creative Arts and Design')
group by subject

5.Show the subject and total number of students who A_STRONGLY_AGREE to question 22 for each of the subjects ‘(8) Computer Science’ and ‘(H) Creative Arts and Design’.

HINT
The A_STRONGLY_AGREE column is a percentage. To work out the total number of students who strongly agree you must multiply this percentage by the number who responded (response) and divide by 100 - take the SUM of that.

SELECT subject,sum(A_STRONGLY_AGREE*response/100)
FROM nss
WHERE question='Q22'
AND subject in ('(8) Computer Science','(H) Creative Arts and Design')
group by subject
  • 从这题的hint里才得知A_STRONGLY_AGREE一栏里的数值是百分比…
    6.Show the percentage of students who A_STRONGLY_AGREE to question 22 for the subject ‘(8) Computer Science’ show the same figure for the subject ‘(H) Creative Arts and Design’.

Use the ROUND function to show the percentage without decimal places.

SELECT subject,round(sum((A_STRONGLY_AGREE/100) //列表中A_STRONGLY_AGREE值为缺少百分号的百分比数,因此要用他除以100得到百分比(XX%)形式,再用结果乘总人数response得到A_STRONGLY_AGREE对应的人数。
*response)/sum(response)*100,0)
FROM nss
WHERE question='Q22'
AND subject in ('(8) Computer Science','(H) Creative Arts and Design')
group by subject

7.Show the average scores for question ‘Q22’ for each institution that include ‘Manchester’ in the name.

The column score is a percentage - you must use the method outlined above to multiply the percentage by the response and divide by the total response. Give your answer rounded to the nearest whole number.

SELECT institution,round(sum(score/100*response)/sum(response)*100,0)
FROM nss
WHERE question='Q22'
AND (institution LIKE '%Manchester%')
group BY institution

8.Show the institution, the total sample size and the number of computing students for institutions in Manchester for ‘Q01’.

  • 题目换句人话说就是:展示①曼切斯特地区的机构名称,②对应机构发出的问题01的样本量(问卷量),③这些样本中,和电脑computer相关的样本量
SELECT institution,sum(sample),sum(case when subject='(8) Computer Science' 
										then sample 
										else 0 end)
FROM nss
WHERE question='Q01'
AND (institution LIKE '%Manchester%')
group by institution
  • 结合前几题的内容,得知答案第三列应该是subject和computer有关的对应的sample总量。用case when语句实现。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值