使用其他字段查找每个组的最大记录数

时间:2014-09-16 18:43:46

标签: sql max inner-join

我有一个名为tgps的表,它有六个字段和数据

Model   ftype   serial  Date        latitude        longitude
Car     B       2142    15/09/2014  S11.59.41.194   W077.07.33.184
Car     A       2123    15/09/2014  S12.15.12.245   W076.55.08.194
Truck   A       2123    16/09/2014  S13.42.48.122   W071.53.22.081
PickUp  C       2111    14/09/2014  S14.36.05.071   W075.11.47.133
PickUp  A       2111    15/09/2014  S14.39.51.245   W075.10.00.000
PickUp  A       2111    14/09/2014  S14.41.14.040   W075.07.12.245
Truck   B       2123    13/09/2014  S14.42.51.092   W075.05.35.133
Car     B       2142    14/09/2014  S14.46.14.040   W070.20.03.030
Truck   A       2123    13/09/2014  S15.54.53.163   W071.11.21.153
Truck   B       2123    16/09/2014  S15.58.40.051   W071.12.48.122
Car     A       2123    16/09/2014  S16.18.06.061   W069.16.24.122
Car     C       2142    13/09/2014  S16.29.27.092   W071.51.48.122

我想为每个DateModelftype选择serial值最高的记录,还需要显示latitudelongitude,所以我的结果应该是这样的:

Model   ftype   serial  Date        latitude        longitude
Car     A       2123    16/09/2014  S16.18.06.061   W069.16.24.122
Car     B       2142    15/09/2014  S11.59.41.194   W077.07.33.184
PickUp  A       2111    15/09/2014  S14.39.51.245   W075.10.00.000
PickUp  C       2111    14/09/2014  S14.36.05.071   W075.11.47.133
Truck   A       2123    16/09/2014  S13.42.48.122   W071.53.22.081
Truck   B       2123    16/09/2014  S15.58.40.051   W071.12.48.122

这需要一个带有连接字段的内部联接(Modelftypeserial),我尝试了以下内容:

SELECT model + ftype + serial, date, latitude, longitude
FROM (
    SELECT model + ftype + serial, max(date) 
    FROM tgps group by model, ftype, serial) 
as xw inner join tgps on tgps.model + tgps.ftype + tgps.serial = xw.model + xw.ftype + xw.serial
and tgps.date = xw.max(date) 

但它不起作用。

4 个答案:

答案 0 :(得分:1)

您可以使用链接到主SELECT的简单子查询来获取每个实体的最大日期:

select 
    Model, ftype, serial, Date, latitude, longitude
from
    tgps T
where
    Date = 
        (
            select
                max(Date)
            from
                tgps
            where
                Model = T.Model
                and ftype = T.ftype
                and serial = T.serial
        )

答案 1 :(得分:0)

您可以使用CTE获取每个型号,ftype和序列的最大日期。然后你可以将它加入到你的表中以获得lat和long这些值。 SQL Fiddle

with CTE as
(select
 model,
 ftype,
 serial,
 max(date) as maxdate
from
tgps
group by
 model,
 ftype,
 serial
)

select
t1.*

from
tgps t1
inner join cte t2
  on t1.model = t2.model
  and t1.ftype = t2.ftype
  and t1.serial = t2.serial
  and t1.date = t2.maxdate

order by t1.model,
t1.ftype

答案 2 :(得分:0)

您可以选择具有更大日期和相同型号的另一行的所有行,ftype和serial not exist

select * from tgps t1
where not exists (
    select 1 from tgps t2
    where t2.model = t1.model
    and t2.ftype = t1.ftype
    and t2.serial = t1.serial
    and t2.date > t1.date
)

或者如果您可以访问分析函数,这将更快

select * from (
  select *, 
  row_number() over (partition by model, ftype, serial order by date desc) rn
from tgps) t1 where rn = 1

答案 3 :(得分:0)

谢谢大家,这就是我解决问题的方法:

SELECT tgps.model + tgps.ftype + tgps.serial,tgps.lat, tgps.lng, tgps.Date FROM tgps INNER JOIN (
    SELECT model, ftype, serial, max(Date) junto
    FROM tgps group by model, ftype, serial) 
as xw on tgps.Date = xw.junto and tgps.serial = xw.serial and tgps.ftype = xw.ftype and tgps.model = xw.model