我要问的项目是给老师发一封电子邮件,询问他们下学期教的是哪些书,这样就可以订购这些书了。我有一个疑问,将即将到来的学期的课程数量与历史教科书订单的课程数量进行比较,只提取本学期正在讲授的课程。那就是我迷路的地方。
我有一个包含以下内容的表格:
数据如下:
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+course number+year组合吐出每一张唱片。
根据样本数据,结果如下:
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我想我应该为每一位老师做一个记录集,在此范围内,为每个课程编号再做一次记录。在课程编号记录集中,我需要系统来确定最高的年数是什么--也许把它存储在变量中?然后取出所有相关的记录,以便如果老师最后一次在课堂上下课时订购了3本书(不管是在2013年还是2012年等等),那么所有三本书都会显示出来。不过,我不确定我是不是在用正确的方式去想唱片集。
到目前为止,我的SQL是基本的,显然不起作用:
SELECT [All].Professor, [All].Course, Max([All].Year)
FROM [All]
GROUP BY [All].Professor, [All].Course;发布于 2013-10-31 22:14:46
使用您的查询作为子查询,并将其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;https://stackoverflow.com/questions/19717229
复制相似问题