这个问题很难想出一个可以理解的标题。 我会尝试用一个例子来解释。如何在两列中选择最大值的记录?
首先我有一个简单的表INFO
Oracle数据库中:
year type message
---- ---- -------
2001 1 cakes are yammy
2003 2 apples are dangerous
2012 2 bananas are suspicious
2005 3 cats are tricky
,我需要选择特定类型的最新消息(例如type = 1
或type = 2
):所以我用
2001 1 cakes are yammy
2012 2 bananas are suspicious
查询(顺便说一句,在这种情况下是否正确?):
select * from INFO i
where year = (select max(year) from INFO i_last where i.type = i_last.type)
and i.type in (1, 2)
但现在我需要在我的INFO表中添加一个新的“季度”列。并按年份和季度选择最新的记录。
year quarter type message
---- ------- ---- -------
2001 2 1 cakes are yammy
2012 3 2 onions are cruel
2012 1 2 bananas are suspicious
2005 1 3 cats are tricky
1型或2的最新记录将是:
2001 2 1 cakes are yammy
2012 3 2 onions are cruel
我无法想象能做到这一点查询,并希望你能帮帮我吧。
在MS Access中是否有等价物?你的回答为人们打开了一个全新的世界。 – BKSpurgeon