Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.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 如何根据同一表中另一字段的最大值获取字段ID_Sql_Ms Access - Fatal编程技术网

Sql 如何根据同一表中另一字段的最大值获取字段ID

Sql 如何根据同一表中另一字段的最大值获取字段ID,sql,ms-access,Sql,Ms Access,我有一个名为ae\u types的表。它包含与我的问题相关的三个字段: aetId This is Auto Increment and is the Primary Key aetProposalType Text field 5 characters long aetDaysToWait Byte data type aetProposalType和aetDaysToWait在一个唯一的密钥中,因此我可以保证不会有两个aetProposalT

我有一个名为
ae\u types
的表。它包含与我的问题相关的三个字段:

aetId               This is Auto Increment and is the Primary Key
aetProposalType     Text field 5 characters long
aetDaysToWait       Byte data type
aetProposalType
aetDaysToWait
在一个唯一的密钥中,因此我可以保证不会有两个
aetProposalType
具有相同的
aetDaysToWait

我要查找的结果是获得每个
aetProposalType
aetDaysToWait
最大的字段的
aetId

下面是我为实现这一点而提出的问题,但在我看来,这可能是不必要的复杂,也不是很漂亮

SELECT ae_types.aetId AS lastEmailId, ae_types.aetProposalType
FROM ae_types INNER JOIN 

(SELECT ae_types.aetProposalType, Max(ae_types.aetDaysToWait) AS MaxOfaetDaysToWait 
FROM ae_types GROUP BY ae_types.aetProposalType)  AS ae_maxDaysToWaitByProposalType 

ON (ae_types.aetDaysToWait = ae_maxDaysToWaitByProposalType.MaxOfaetDaysToWait) 
AND (ae_types.aetProposalType = ae_maxDaysToWaitByProposalType.aetProposalType);
有哪些替代方案,为什么会更好


PS如果您有任何问题,请提问,我很乐意尝试提供答案。

我也会这样做

select a.aetId, a.aetProposalType, a.aetDaysToWait
    from ae_types a
        inner join (select aetProposalType, max(aetDaysToWait) as MaxDays
                    from ae_types
                    group by aetProposalType) sq
            on a.aetProposalType = sq.aetProposalType
                and a.aetDaysToWait = sq.MaxDays

既然你问的是“替代方案”。。。每当你把自己绑在椒盐卷饼上时,你必须重新评估。您正在尝试实施什么业务规则?很少有理由想知道关于自动递增id的任何信息。
ae_types
表中的每条记录都代表一种事件类型。这些事件按
aetDaysToWait
字段排序,并按
aetProposalType
字段分组。我需要找到每种类型的最后一个事件的id。然后,我检查当前事件(来自另一个表的数据)是否是最终事件,如果是,则将其标记为最终事件。