简体   繁体   English

行之间的差异 Mysql Query

[英]Difference between rows Mysql Query

I have one table which is having four fields:我有一张有四个字段的表:

trip_paramid, creation_time, fuel_content,vehicle_id

I want to find the difference between two rows.In my table i have one field fuel_content.Every two minutes i getting packets and inserting to database.From this i want to find out total refuel quantity.If fuel content between two packets is greater than 2,i will treat it as refueling quantity.Multiple refuel may happen in same day.So i want to find out total refuel quantity for a day for a vehicle.I created one table schema&sample data in sqlfiddle.我想找到两行之间的差异。在我的表中,我有一个字段fuel_content。每两分钟我都会收到数据包并插入到数据库中。由此我想找出总加油量。如果两个数据包之间的燃料含量大于2,我将其视为加油量。同一天可能会发生多次加油。所以我想找出一辆车一天的总加油量。我在sqlfiddle中创建了一个表模式和示例数据。 Can anyone help me to find a solution for this.here is the link for table schema.. http://www.sqlfiddle.com/#!2/4cf36任何人都可以帮我找到解决方案。这里是表模式的链接.. http://www.sqlfiddle.com/#!2/4cf36

Here is a good query.这是一个很好的查询。

Parameters (vehicle_id=13) and (date='2012-11-08') are injected in the query, but they are parameters to be modified.参数(vehicle_id=13)和(date='2012-11-08')被注入到查询中,但它们是要修改的参数。

You can note that have I chosen an expression using creation_time<.. and creation_time>.. in instead of DATE(creation_time)='...' , this is because the first expression can use indexes on "creation_time" while the second one cannot.您可以注意到我选择了一个使用creation_time<..creation_time>..的表达式而不是DATE(creation_time)='...' ,这是因为第一个表达式可以使用“creation_time”上的索引,而第二个表达式不能。

SELECT
SUM(fuel_content-prev_content) AS refuel_tot
, COUNT(*) AS refuel_nbr
FROM (
  SELECT
  p.trip_paramid
  , fuel_content
  , creation_time
  , (
    SELECT ps.fuel_content
    FROM trip_parameters AS ps
    WHERE (ps.vehicle_id=p.vehicle_id)
    AND (ps.trip_paramid<p.trip_paramid)
         ORDER BY trip_paramid DESC
         LIMIT 1
    ) AS prev_content
  FROM trip_parameters AS p
  WHERE (p.vehicle_id=13)
  AND (creation_time>='2012-11-08')
  AND (creation_time<DATE_ADD('2012-11-08', INTERVAL 1 DAY))
  ORDER BY p.trip_paramid
) AS log
WHERE (fuel_content-prev_content)>2

Test it:测试一下:

select sum(t2.fuel_content-t1.fuel_content) TotalFuel,t1.vehicle_id,t1.trip_paramid as rowIdA,
t2.trip_paramid as rowIdB,
t1.creation_time as timeA,
t2.creation_time as timeB, 
t2.fuel_content fuel2,
t1.fuel_content fuel1,
(t2.fuel_content-t1.fuel_content)  diffFuel
from trip_parameters  t1, trip_parameters  t2
where t1.trip_paramid<t2.trip_paramid 
and t1.vehicle_id=t2.vehicle_id 
and t1.vehicle_id=13
and t2.fuel_content-t1.fuel_content>2 
order by rowIdA,rowIdB

where (rowIdA,rowIdB) are all possibles tuples without repetition, diffFuel is the difference between fuel quantity and TotalFuel is the sum of all refuel quanty.其中 (rowIdA,rowIdB) 是没有重复的所有可能元组,diffFuel 是燃料量之间的差异,TotalFuel 是所有燃料量的总和。

The query compare all fuel content diferences for same vehicle(in this example, for vehicle with id=13) and only sum fuel quantity when the diff fuel is >2.该查询比较同一车辆的所有燃料含量差异(在此示例中,对于 id=13 的车辆),并且仅当差异燃料 >2 时才汇总燃料量。

Regards.问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM