|
建议别用SQL的语法套到Access里,除了部分语法不一样之外,其对数据的处理方式也是不一样的。例如SQL里常用的是子查询和嵌套查询,而子查询在Access里执行效率可能很低(例如In Select 类型),而用得更多是左联接、右联接等等。
第一个可以简化为:SELECT stuscore.name, Sum(stuscore.score) AS 总分 FROM stuscore GROUP BY stuscore.name ORDER BY Sum(stuscore.score) DESC;
第二个类似,增加个stuid字段而已。
第三个:SELECT TOP 2 stuscore.stuid, stuscore.name, stuscore.subject, stuscore.score FROM stuscore GROUP BY stuscore.stuid, stuscore.name, stuscore.subject, stuscore.score ORDER BY stuscore.score DESC;
第四个:SELECT DISTINCT stuscore.name, Avg(stuscore.score) AS score之平均值 FROM stuscore GROUP BY stuscore.name;
第五个:SELECT DISTINCT stuscore.subject, DMax("Score","stuscore","subject='" & [subject] & "'") AS 成绩,DLookUp("name","stuscore","subject='" & [subject] & "' and score=" & [成绩]) AS nameFROM stuscore;
第六个:SELECT TOP 3 stuscore.subject, DMax("score","stuscore","subject='" & [subject] & "'") AS score,DLookUp("name","stuscore","subject='" & [subject] & "' and score=" & [score]) AS name FROM stuscore;
第七个:TRANSFORM Sum(stuscore.score) AS score SELECT stuscore.stuid AS 学号, stuscore.name AS 姓名, DSum("Score","Stuscore","name='" & [name] &"'")/DCount("Score","Stuscore","name='" & [name] & "'") AS 平均分, Sum(stuscore.score) AS 总分 FROM stuscore GROUP BY stuscore.stuid, stuscore.name, DSum("Score","Stuscore","name='" & [name] & "'")/DCount("Score","Stuscore","name='" & [name] & "'") PIVOT stuscore.subject;
第八个不变。
第九个:建议先建立一个含有自动编号的空白表,按降序将数据追加进去,自动编号即为排名。即便有时候可能编号有变化,用Dcount也可以很轻易得到排名。
第十个:建立第一个查询,用降序Top3获得前三名学生(科目的条件为“数学”),再以此为数据源升序获取Top2,即为第二、三名。
第十一个:参考第九个。
第十二个:用IIF来写自定义字段,例如:SELECT stuscore.subject, Sum(IIf([Score]<60,1,0)) AS 不及格人数, Sum(IIf([Score]>=60 And [Score]<=80,1,0)) AS 良好, Sum(IIf([Score]>80 And [Score]<100,1,0)) AS 优秀 FROM stuscore GROUP BY stuscore.subject, stuscore.score;
第十三个:不好意思。这个不是查询,VBA代码很容易实现。但鉴于你的理解能力,暂时先不说了。 |
|