按最新日期列汇总表,仅从两列中选择一个值

时间:2016-10-04 10:31:18

标签: sql ms-access

我正在MS中访问跟踪设备(设备参考在第一列:EquipmentID,某些设备可以连接到其他设备或单独) http://sqlfiddle.com/#!6/e0a56/1/0 该表与此类似

EquipmentID | AuxiliaryID | Location | LastDate | OtherInfo
_____________________________________________________________
Truck 1     | Trailer 2   | Town 1   |01/02/2016| Info 1     |
Truck 2     | Trailer 3   | Town 2   |01/03/2016| Info 2     |
Truck 3     |             | Town 3   |01/04/2016| Info 3     |
Truck 4     |             | Town 4   |01/05/2016| Info 4     |
Trailer 1   |             | Town 5   |01/06/2016| Info 5     |
Trailer 2   |             | Town 6   |01/01/2016| Info 6     |
Trailer 3   |             | Town 7   |01/08/2016| Info 7     |

我试图弄清楚设备的状态(最新日期指定的最后位置)。我想要实现的目标如下表所示:

EquipmentID | AuxiliaryID | Location | LastDate | OtherInfo
_____________________________________________________________
Truck 1     | Trailer 2   | Town 1   |01/02/2016| Info 1
Truck 2     |             | Town 2   |01/03/2016| Info 2
Truck 3     |             | Town 3   |01/04/2016| Info 3
Truck 4     |             | Town 4   |01/05/2016| Info 4
Trailer 1   |             | Town 5   |01/06/2016| Info 5
Trailer 3   |             | Town 7   |01/08/2016| Info 7
  • 如果辅助ID中没有来自EquimentID的值,则返回该行,例如:Truck 3,Truck 4,Trailer 3。返回该行
  • 如果在AuxliaryID(预告片2,预告片3)中存在来自EquipmentID的值,请比较日期(LastDate)以检查最新日期。
    • 预告片2出现在第一行和第六行,但第一行有最新日期,所以不要返回第六行
    • 预告片3出现在第二行和最后一行,但最后一行是最新的日期,因此返回最后一行并从第一行中删除辅助ID

我能在SQL查询中执行此操作吗?

3 个答案:

答案 0 :(得分:1)

您可以通过查找最大日期并选择包含该信息的行来选择最后一行。

这是一种方法:

select t.*
from Trucks as t
where t.LastDate = (select max(t2.LastDate)
                    from Trucks as t2
                    where t2.EquipmentId = t.EquipmentId
                   );

答案 1 :(得分:1)

请试一试。

SELECT
t1.EquipmentID, 
t2.EquipmentID as AuxiliaryID, 
t1.Location, 
t1.LastDate, 
t1.OtherInfo
FROM Trucks t1
left join Trucks t2 on t1.AuxiliaryID = t2.EquipmentID and t1.LastDate > t2.LastDate
inner join (
  select EquipmentID, max(LastDate) as MaxLastDate
  from (
    select EquipmentID, LastDate
    from Trucks
    UNION
    select AuxiliaryID, LastDate
    from Trucks where AuxiliaryID is not null) a
    group by EquipmentID) maxdates 
  on t1.EquipmentID = maxdates.EquipmentID and t1.LastDate = maxdates.MaxLastDate;

答案 2 :(得分:1)

试试这个:

select
  max(case when src = 'e' then p.equipmentID end) as equipID,
  max(case when src = 'a' then p.EquipmentID end) as auxID,
  Location,
  t.LastDate,
  OtherInfo
from
  trucks t inner join
  (select
    src,
    EquipmentID,
    LastDate,
    ROW_NUMBER() OVER(PARTITION BY EquipmentID ORDER BY lastDate desc) rn
   from
    (SELECT 'e' as src, EquipmentID, LastDate FROM Trucks
     union all
     SELECT 'a' as src, AuxiliaryID, LastDate FROM Trucks 
            where AuxiliaryID is not null) u) p on
   rn = 1 and
   t.lastdate = p.LastDate and
   (t.equipmentid = p.equipmentid  or
    t.auxiliaryID = p.equipmentid ) 
group by Location, t.LastDate, OtherInfo
order by t.LastDate

sqlFiddle