如何比较相关表中的日期并使用它来选择查询中的相关记录? SQL/MS 访问

发布于 2025-01-01 01:53:56 字数 843 浏览 0 评论 0原文

课程的细节会随着时间的推移而变化。我需要能够访问课程运行时这些详细信息的最新版本。

我的表格设置如下(仅显示相关字段):

课程详细信息

  • ClassID(主键)
  • CourseID(外键)

  • 开始日期

课程

  • CourseID(主键)

课程版本

  • CourseVersionID(主键)
  • CourseID (外键)

  • 课程版本开始日期

我想在包含以下列的查询中为每个唯一 ClassID 显示一条记录:

  • ClassID< /p>

  • 紧邻开始日期下方的课程版本开始日期(请参阅下面的示例)

课程版本开始日期 (1) = 01/01/2010

课程版本开始日期 (2) = 01/11/2011

开始日期在 (1) 和 (2) 之间 ~ 返回值为 01/01/2010

开始日期大于 (2) ~ 返回值为 01/11/2011

开始日期小于(1)~返回值无,该记录不显示。

  • CourseVersionID

无需太多 SQL 知识(即在 Access 设计视图中)即可使用的解决方案是首选,但不是必需的。

The details of a course change over time. I need to be able to access the version of these details that was current when the course was run.

My tables are setup as follows (only relevant fields are shown):

Class Details

  • ClassID (Primary key)
  • CourseID (Foreign key)

  • Commencement Date

Courses

  • CourseID (Primary key)

Course Versions

  • CourseVersionID (Primary key)
  • CourseID (Foreign key)

  • Course Version Start Date

I want to show one record per unique ClassID in a query with the following columns:

  • ClassID

  • The Course Version Start Date immediately below the Commencement Date (see examples below)

Course Version Start Date (1) = 01/01/2010

Course Version Start Date (2) = 01/11/2011

Commencement Date is between (1) and (2) ~ The returned value is 01/01/2010

Commencement Date is greater than (2) ~ The returned value is 01/11/2011

Commencement Date is less than (1) ~ The returned value is nothing, this record doesn't show up.

  • CourseVersionID

Solutions that can be used without much SQL knowledge (ie within the Access design view) are prefered but not necessary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心如荒岛 2025-01-08 01:53:58

在 SQL 中:

SELECT Class.ID, 
  Max(CourseVersions.CourseVersionStartDate) AS CourseVersionStartDate
FROM 
    (Course INNER JOIN Class ON Course.CourseId = Class.CourseId)
INNER JOIN 
    CourseVersions ON Course.CourseId = CourseVersions.CourseId
WHERE 
    (((Class.CommencementDate) > CourseVersions.CourseVersionStartDate))
GROUP BY Class.ID;

或者,由于您更喜欢访问查询设计器:

Access Query Design

In SQL:

SELECT Class.ID, 
  Max(CourseVersions.CourseVersionStartDate) AS CourseVersionStartDate
FROM 
    (Course INNER JOIN Class ON Course.CourseId = Class.CourseId)
INNER JOIN 
    CourseVersions ON Course.CourseId = CourseVersions.CourseId
WHERE 
    (((Class.CommencementDate) > CourseVersions.CourseVersionStartDate))
GROUP BY Class.ID;

Or, since you prefer the access query designer:

Access Query Design

紫南 2025-01-08 01:53:58

不完全是查询设计窗口,尽管可以在那里查看,即使没有符合您第五个要求的日期,它也会返回一行。

SELECT [class details].classid,
       [class details].courseid,
       [class details].[commencement date],
       (SELECT TOP 1 [course version start date]
        FROM   [course versions]
        WHERE  courseid = courses.courseid
               AND
       [class details].[commencement date] > [course version start date]
        ORDER  BY [course version start date] DESC) AS commdate
FROM   [class details]
       INNER JOIN courses
         ON [class details].courseid = courses.courseid; 

Not quite query design window, though it can be viewed there, and it will return a line even if there is no date as per your fifth requirement.

SELECT [class details].classid,
       [class details].courseid,
       [class details].[commencement date],
       (SELECT TOP 1 [course version start date]
        FROM   [course versions]
        WHERE  courseid = courses.courseid
               AND
       [class details].[commencement date] > [course version start date]
        ORDER  BY [course version start date] DESC) AS commdate
FROM   [class details]
       INNER JOIN courses
         ON [class details].courseid = courses.courseid; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文