VBA/SQL记录集
问题描述:
我在问的项目是给老师发一封电子邮件,询问他们为下一学期教授的课程使用什么书,以便订购书籍。我有一个查询,将即将到来的学期课程的课程编号与历史教科书课程的课程编号进行比较,仅提取本学期所教授的课程。那就是我迷失的地方。VBA/SQL记录集
我有一个包含以下内容的表:
- 教授
- 课程编号
- 年
- 书名
的数据是这样的:
professor year course number title
--------- ---- ------------- -------------------
smith 13 1111 Pride and Prejudice
smith 13 1111 The Fountainhead
smith 13 1222 The Alchemist
smith 12 1111 Pride and Prejudice
smith 11 1222 Infinite Jest
smith 10 1333 The Bible
smith 13 1333 The Bible
smith 12 1222 The Alchemist
smith 10 1111 Moby Dick
johnson 12 1222 The Tipping Point
johnson 11 1333 Anna Kerenina
johnson 10 1333 Everything is Illuminated
johnson 12 1222 The Savage Detectives
johnson 11 1333 In Search of Lost Time
johnson 10 1333 Great Expectations
johnson 9 1222 Proust on the Shore
下面是我需要的代码“纸上”: 由教授组记录。确定该组中的每个唯一课程编号,并按课程编号分组记录。对于每个独特的课程编号,确定相关的最高年份。然后用该教授+课程编号+年份组合吐出每条记录。
随着样本数据,结果将是:
professor year course number title
--------- ---- ------------- -------------------
smith 13 1111 Pride and Prejudice
smith 13 1111 The Fountainhead
smith 13 1222 The Alchemist
smith 13 1333 The Bible
johnson 12 1222 The Tipping Point
johnson 11 1333 Anna Kerenina
johnson 12 1222 The Savage Detectives
johnson 11 1333 In Search of Lost Time
我想我应该为每个老师一个记录集,并在那,另一个记录每门课程数设置。在课程编号记录集中,我需要系统来确定最高年份编号是什么 - 可能将其存储在变量中?然后抽出所有相关记录,这样如果老师在最后一次教他们上课时(无论是2013年还是2012年等),他们会订购3本书,所有三本书都会显示。不过,我不确定我是否正确地考虑了唱片集。
我的SQL到目前为止是基本的和显然不工作:
SELECT [All].Professor, [All].Course, Max([All].Year)
FROM [All]
GROUP BY [All].Professor, [All].Course;
答
使用您的查询作为子查询和INNER JOIN
它回到[ALL]
表来筛选行。
SELECT
a.Professor,
a.Year,
a.Course,
a.title
FROM
[ALL] AS a
INNER JOIN
(
SELECT [All].Professor, [All].Course, Max([All].Year) AS MaxOfYear
FROM [All]
GROUP BY [All].Professor, [All].Course
) AS sub
ON
a.Professor = sub.Professor
AND a.Course = sub.Course
AND a.Year = sub.MaxOfYear;
非常感谢,汉斯。完美的作品。 – intruesiive