是否可以使用max语句从一列的MAX中的查询中选择某些行?
我尝试了几件不起作用的东西。我尝试了max语句。我也看到Interbase可能不支持我在SQL Server中常用的东西 - 子选择。我错了。我想我们正在使用Interbase XE3。
说明:
SELECT PM.GUID, PM.PID,
PM.MISCID,
CAST((PM.YEAR) AS NUMERIC) AS THEYEAR
FROM PMASTER PM
INNER JOIN SEL SL
ON SL.LGID = PM.PID
ORDER BY PM.PID,
THEYEAR
返回数据
1244 1 21 2013
3444 1 21 2014
9888 1 21 2015
3244 3 45 2014
5144 3 45 2015
6588 3 45 2016
3324 6 73 2014
5454 6 73 2015
6758 6 74 2016
我希望年度最大栏目: 返回所需数据:
9888 1 21 2015
6588 3 45 2016
6758 6 74 2016
由于
瑞克
答案 0 :(得分:0)
SELECT PM.GUID, PM.PID,
PM.MISCID,
CAST((PM.YEAR) AS NUMERIC) AS THEYEAR
FROM PMASTER PM
INNER JOIN SEL SL
ON SL.LGID = PM.PID
GROUP BY PM.PID
HAVING PM.YEAR = MAX(PM.YEAR);
答案 1 :(得分:0)
好的,我明白了。与Stackoverflow和Web上其他地方的几个语句相反,Interbase支持子选择。这是我的工作解决方案:
SELECT PM.GUID, PM.PID, PM.MISCID, PM.YEAR FROM PMASTER PM
INNER JOIN SEL SL ON SL.LGID = PM.PID
WHERE AND PM.YEAR IN
(SELECT MAX(PMS.YEAR) FROM PMASTER PMS WHERE PMS.PID = PM.PID)
ORDER BY PM.PID, PM.YEAR
希望能帮助别人。