【问题标题】:SQL COUNT commandSQL 计数命令
【发布时间】:2011-04-29 13:13:40
【问题描述】:
CREATE TABLE Student_Exam(
  Exam_ID INTEGER,
  S_ID INTEGER,
  Primary Key (Exam_ID, S_ID),
  Foreign Key (Exam_ID) References Exams(Exam_ID),
  Foreign Key (S_ID) References Students(S_ID),
  Pass TEXT
);

CREATE TABLE Students(
  S_ID INTEGER PRIMARY KEY,
  First_Name TEXT,
  Surname TEXT
);

CREATE TABLE Exams(
  Exam_ID INTEGER PRIMARY KEY,
  Date_Taken DATE
);

我该如何纠正这个问题?

SELECT MAX(Students.S_ID) AS S_ID, **count(Pass="Yes")** AS No_of_Exams_Taken
FROM Student_Exam, Students, Exams
WHERE Students.S_ID=Student_Exam.S_ID And Exams.Exam_ID=Student_Exam.Exam_ID And (Exams.Date_Taken)>=#1/1/2010# And (Exams.Date_Taken)<=#12/31/2010#
GROUP BY Student_Exam.S_ID;

我想计算每个学生通过的考试次数? SELECT 命令中的计数应该如何?

SELECT Student.S_ID, COUNT(*) AS Final_Exam_Level 从学生,考试,Student_Exam WHERE (Student.S_ID)=Student_Exam.S_ID 和 ((Exams.Exam_ID)=Student_Exam.Exam_ID) 并且(Exams.Date_Taken)

以上是正确的查询

【问题讨论】:

  • 分组和总结是报告做得很好的事情,所以你不应该在报告中这样做(好吧,你可能需要这样做,但很可能不需要)。停止构建报表,而是构建一个返回正在汇总的数据的 SQL SELECT,然后尝试将其用作报表的 Recordsource。

标签: sql ms-access count if-statement


【解决方案1】:
select s.s_id as student_id, count(*)
FROM students s
left join student_exam se on s.s_id = se.s_id and se.pass='yes'
left join exam e on se.exam_id = e.exam_id
group by s.s_id

我建议您将 pass 更改为非文本字段。如果它只有 yes 和 no 那么它应该是布尔值。

【讨论】:

  • 我不能使用左连接或右连接或任何连接,然后我想将它组合到另一个查询中。我不允许使用布尔值,我不是计算机学生,我只能使用 TEXT、NUMBER、INTEGER、DATE 和 TIME
  • 然后尝试在该字段中使用值为 0 或 1 的 Integer。
  • 因为我是商科学生,我们不允许
  • 顺便说一句,您正在使用不使用 join 语句的语法“进行连接”——它只限于所谓的内部连接。
  • @Hogan 说了什么。也就是说,使用 WHERE 子句实现的隐式 JOIN 与使用单词 JOIN 的显式 JOIN 一样。除非本课旨在教您隐式 JOIN 的功能和限制,否则对使用 JOIN 的限制是无意义的。
【解决方案2】:

此版本回答了如何根据条件进行计数的具体问题。作为奖励,它还将提供参加的考试次数。

select
   s.s_id as student_id
  ,count(case when se.pass='Yes' then 1 end) as exams_passed
  ,count(*) as exams_taken
from
   Students s
   join Student_Exam se on s.s_id = se.s_id
   join Exam e on se.exam_id = e.exam_id
where
   e.date_taken between #1/1/2010# and #12/31/2010#
group by
   s.s_id

根据您的其他 cmets,避免 join 的版本将是:

select
   s.s_id as student_id
  ,count(case when se.pass='Yes' then 1 end) as exams_passed
  ,count(*) as exams_taken
from
   Students s
  ,Student_Exam se
  ,Exam e
where
   s.s_id = se.s_id
   and se.exam_id = e.exam_id
   and e.date_taken between #1/1/2010# and #12/31/2010#
group by
   s.s_id

或者,这里有一个更简单的版本,case 并不那么花哨:

select
   s.s_id as student_id
  ,count(*) as exams_passed
from
   Students s
  ,Student_Exam se
  ,Exam e
where
   s.s_id = se.s_id
   and se.exam_id = e.exam_id
   and e.date_taken between #1/1/2010# and #12/31/2010#
   and se.pass = 'Yes'
group by
   s.s_id

【讨论】:

  • 计数(Student_Exam.pass='Yes' then 1 end)作为 Exams_Passed。请注意,Pass 在联合表中。而且我不明白当 student_exam='Yes' 然后 1 结束时的情况。但无论如何我尝试了它并没有奏效。您认为您可以提供进一步的帮助吗?
  • 糟糕。感谢您的指正。当se.pass='Yes' 条件为真时,case 表达式产生1,否则产生NULL。 Count 将只计算非NULL 行,为您提供通过考试的计数。
  • 没有空行。是或否。
  • 可能是这样,如果不满足任何条件,case 表达式将为您提供NULL。使用case 的替代品将是sum(case when se.pass='Yes' then 1 else 0 end) as exams_passed,它的作用几乎相同。
  • 啊。显然 Access 不支持case。试试sum(iif(Student_Exam.Pass='Yes',1,0))
猜你喜欢
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-22
  • 2012-03-06
  • 2020-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多