【问题标题】:Get the row with the max date value with criteria - access 2007/2010使用条件获取具有最大日期值的行 - 访问 2007/2010
【发布时间】:2014-03-31 06:16:38
【问题描述】:

我从中获取所有数据的主表是“RequestTable”(我将其缩小以使其更容易),其中我有:

ID_student
ID_professor
Date (and the three altogether are primary keys)
changeprofessor-note - if student wants to change the professor 
                       then he/she should write in that field a sentence 
                       why he/she wants to do the change

professor-reject-note - if the professor is not happy about the work of 
                        the student, then he can choose not to mentor that 
                        student anymore, leaving him without a mentor and the 
                        student should choose another mentor later.

ID-seminar- after choosing a mentor the students 
            can choose the seminar they want to work on

changeofSeminar-note - if the student wants to change the seminar 
                       then they need to write the reason why in here 
                       (then the ID of the new seminar should be written in 
                       the ID seminar field also)

IDapprove-reject - all approving or rejecting is going through this field

我最初的理论是学生可以连续选择导师和研讨会,但现在看起来太复杂了,因为我不知道如何在更换导师、拒绝导师、更换研讨会等之后让一切正常进行。

我设定了一个更舒服的理论,所有的学生都需要先选择导师。这样我就可以在需要时更容易获得指导数据。我在“ID_seminar”和“changeofseminar-note”下的查询中设置了“is null”,因为仅研讨会部分的任何更改都不会影响学生选择导师/教授并获得批准的行。

我实现了你的代码并得到了这个:

SELECT [requesttable].ID_Student, Max([requesttable].Datum) AS MaxOfDatum,                                                                        First([requesttable].ID_Profesor) AS ID_Profesor, [requesttable].ID_status_odobrenja
FROM [requesttable]
WHERE ((([requesttable].ID_Student) Not In (SELECT  [ID_Student]

FROM  [requesttable]

WHERE  [IDapprove-reject] IS NOT NULL )))
GROUP BY [requesttable].ID_Student, [requesttable].IDapprove-reject, [requesttable].changeseminar-note, [requesttable].ID_seminar
HAVING ((([requesttable].IDapprovereject)=1) AND (([requesttable].changeseminar-note) Is Null) AND (([requesttable].Id_seminar) Is Null))
ORDER BY [requesttable].ID_Student, Max([requesttable].Datum), First([requesttable].ID_Profesor), [requesttable].IDapproved-reject;

我得到:

 3   12   1
15   11   1
55    5   1

我需要:

 3   6   1
15   6   1
52   5   1 - after being rejected by mentor 10, 
             the student choose another mentor (id 5) and got approved.
55   5   1

以下旧信息:

我的查询到此为止,另外两个数据被设置为只显示具有空值的行来得到这个:

ID student Id professor date       professor-reject-note ID accept/reject
3          12           12.11.2012 null                     1 
3           6           13.11.2012 null                     1
52         10           12.11.2012 null                     1 
52         10           15.11.2012 NOT null                 1 
55          5           12.11.2012 null                     1 

我希望我的结果是

3           6           12.10.2013 null                     1
15          6           7.1.2013   null                     1
55          5           12.11.2012 null                     1

完全排除 StudentID 52,因为教授拒绝说明意味着教授不想再指导学生。另外我对该选项中的 ID 接受/拒绝编号有疑问,也许我可以将其设置为 2 而不是 1 以使其更容易。 1 表示接受,2 表示拒绝,但如果我将其设置为 2 并排除整行,我仍然无法摆脱其他 ID 52 行。我对此有点困惑,不知道如何使它工作。

如果我将日期设置为 maxdate 并且 Id 教授按 FIRST 分组,我几乎得到了我想要的,所有数据都是正确的,除了学生 ID 52 仍然存在 - 两行。

【问题讨论】:

    标签: ms-access-2007 ms-access-2010


    【解决方案1】:

    你可以使用:

    SELECT t.[id student],
           t.[id professor],
           t.DATE,
           t.[professor-reject-note],
           t.[id accept/reject]
    FROM   atable t
    WHERE  t.[id student] NOT IN 
        (SELECT [id student]
         FROM   atable
         WHERE  [professor-reject-note] IS NOT NULL) 
    

    您的字段/列名可以做一些工作。

    【讨论】:

    • 感谢您的回答。我想我在查询设计模式下做了同样的事情。但是现在我尝试使用您的代码(我不确定我是否做对了所有事情,因为我在此查询中连接了许多表)但仍然向我显示另一个 ID Student 52 行,即拒绝中的 Is Null-请注意,我想完全排除 ID_student 52 以及与 ID 52 处于相同情况的每个 ID。
    • 恐怕如果你简化你展示的数据,就很难提供答案。上面的代码将从您的问题中显示的数据表中排除 ID 52。我在发布之前进行了测试。
    • 雷姆!我不知道你是否因为在这里帮助别人而得到报酬,但不管怎样,我欠你的不仅仅是一个巨大的感谢。我想我现在明白了,一切似乎都正常,没有你的帮助我无法弄清楚。多谢了! :)
    • 我为 ID_student 52 添加了一位新导师,因为他被 ID_professor 10 拒绝了,现在他根本不想露面。如果他稍后找到具有新导师 ID 的导师,我希望他出现。我需要它用于“当前指导”表,它可以让我成为每个学生的当前指导者(如果他有的话)。
    • Stackoverflow 上没有人付费(开发人员除外),所有帮助都是免费提供的。您应该发布修改后的数据,显示您现在拥有的数据。
    猜你喜欢
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多