使用条件获取具有最大日期值的行 - 访问2007/2010

时间:2014-03-30 16:37:33

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

我的主表格,我从中获取所有数据的是“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仍在那里 - 两行。

1 个答案:

答案 0 :(得分:0)

您可以使用:

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) 

您的字段/列名称可以用于某些工作。

相关问题