Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql MS Access为每个GroupID选择最近的日期_Sql_Ms Access_Vba - Fatal编程技术网

Sql MS Access为每个GroupID选择最近的日期

Sql MS Access为每个GroupID选择最近的日期,sql,ms-access,vba,Sql,Ms Access,Vba,我有三个MS访问表,tblGroup、tblItem和tblStatus: [tblItem] ItemID ItemName ItemDate GroupID StatusID [tblGroup] GroupName StatusID [tblStatus] StatusID StatusName 我正在尝试编写一个VBA SQL查询,以便为tblGroup的StatusID等于“1”的每个GroupID选择tblItem中具有最新日期(即Max(ItemDate))的所有行 我一直在

我有三个MS访问表,tblGroup、tblItem和tblStatus:

[tblItem]
ItemID
ItemName
ItemDate
GroupID
StatusID

[tblGroup]
GroupName
StatusID

[tblStatus]
StatusID
StatusName
我正在尝试编写一个VBA SQL查询,以便为tblGroup的StatusID等于“1”的每个GroupID选择tblItem中具有最新日期(即Max(ItemDate))的所有行

我一直在尝试以下的变化(见下文),但我不知道如何根据另一个表过滤结果,即tblGroup的StatusID等于另一个表中的“1”

DoCmd.OpenForm "frmItem"
Forms!frmItem.frmItemSubform.Form.RecordSource = "SELECT tblItem.ItemID, Max(tblItem.ItemDate) FROM tblItem GROUP BY tblItem.ItemID, tblItem.ItemDate"
Forms!frmItem.frmItemSubform.Requery
任何帮助都将不胜感激


乔治

如果我理解正确,那么这几乎是您需求的直接翻译:

select i.*
from tblItem as i 
where i.date = (select max(i2.date)
                from tblItems as i2 inner join
                     tblGroup as g
                     on i2.GroupId = g.GroupId
                where i2.GroupId = i.GroupId and g.StatusId = 1
               );
或者,或者:

select i.*
from tblItem as i inner join
     (select i.GroupId, max(i2.date) as date
      from tblItems as i2 inner join
           tblGroup as g
           on i2.GroupId = g.GroupId
      where g.StatusId = 1
      group by GroupId
     ) gmax
     on gmax.GroupId = i.GroupId and gmax.date = i2.date;

这些版本并不完全相同。如果一个项目的不同组之间存在日期关联,则第一种方法可能不起作用。这解决了这个问题。

如果我理解正确,那么这几乎是对您需求的直接翻译:

select i.*
from tblItem as i 
where i.date = (select max(i2.date)
                from tblItems as i2 inner join
                     tblGroup as g
                     on i2.GroupId = g.GroupId
                where i2.GroupId = i.GroupId and g.StatusId = 1
               );
或者,或者:

select i.*
from tblItem as i inner join
     (select i.GroupId, max(i2.date) as date
      from tblItems as i2 inner join
           tblGroup as g
           on i2.GroupId = g.GroupId
      where g.StatusId = 1
      group by GroupId
     ) gmax
     on gmax.GroupId = i.GroupId and gmax.date = i2.date;
这些版本并不完全相同。如果一个项目的不同组之间存在日期关联,则第一种方法可能不起作用。这解决了这个问题