SELECT a.spid,z.max_date,a.kcshl FROM spkfjc a inner join (SELECT a.spid AS spid ,MAX(b.rq) AS max_date FROM spkfjc a JOIN splsk b ON a.spid=b.spid WHERE a.kcshl>0 AND a.kcshl=b.jcshl GROUP BY a.spid )z on a.spid=z.spid
我想要获取每个唯一的组,其中date是max,而check大于0.因此返回结果如下所示:
- group date checks
- 2 1/1/2013 800
- 1 1/3/2013 700
- 3 1/5/2013 200
解决方法
- SELECT group,MAX(date) as max_date
- FROM table
- WHERE checks>0
- GROUP BY group
这样做可以获得最大的日期。将它加入到数据中以获取其他列:
- Select group,max_date,check
- from table t
- inner join
- (SELECT group,MAX(date) as max_date
- FROM table
- WHERE checks>0
- GROUP BY group)a
- on a.group = t.group and a.max_date = date
内连接功能作为过滤器,仅获取最大记录。
FYI,您的列名可怕,请勿对列(组,日期,表)使用保留字。
If opportunity doesn’t knock, build a door