如何从MYSQL中的奇数行中减去连续的偶数行?

时间:2016-11-08 16:18:59

标签: mysql

我有以下mysql表的快照。

ID      NAME          TIME                     TYPE      
911018  AAA 2016-11-07 08:10:04.000000       0   
911018  AAA 2016-11-07 10:10:55.000000       1  
911018  AAA 2016-11-07 10:15:31.000000       0  
911018  AAA 2016-11-07 11:15:50.000000       1  
911018  AAA 2016-11-07 11:49:21.000000       0  
911018  AAA 2016-11-07 12:13:02.000000       1  
911018  AAA 2016-11-07 12:14:26.000000       0  
911018  AAA 2016-11-07 13:14:33.000000       1  
911018  AAA 2016-11-07 13:21:26.000000       0  
911018  AAA 2016-11-07 13:51:43.000000       1  
911018  AAA 2016-11-07 13:53:29.000000       0  
911018  AAA 2016-11-07 15:44:45.000000       1  
911018  AAA 2016-11-07 15:46:25.000000       0  
911018  AAA 2016-11-07 16:45:44.000000       1  
911018  AAA 2016-11-07 17:01:58.000000       0  
911018  AAA 2016-11-07 18:07:18.000000       1  
911018  AAA 2016-11-07 18:09:00.000000       0  
911018  AAA 2016-11-07 19:15:33.000000       1  

TYPE 0表示 IN TYPE 1表示 OUT

有没有办法用第1行(2-1)和第4行(4-3)减去第2行,依此类推..然后我可以添加它并将其存储在同一个表或其他表中表可能(第2行 - 第1行)+(第4行 - 第3行)+(第6行-5行)等等,只有当Type在0,1,0,1,0对中时才应该这样做, 1 ...

如果该对是0,0,1,0,1,0 ...那么它应该采用第一个相似的IF对是0,1,0,1,1,0,1 ..然后它应该采取第二个1

结果将类似于

911018 AAA 2016-11-07 02:00:00.000000 (10:10:04.0000000 - 08:00:00.000000)
911018 AAA 2016-11-07 01:00:00.000000 (11:15:00.0000000 - 10:15:00.000000)
911018 AAA 2016-11-07 00:24:00.000000
911018 AAA 2016-11-07 01:00:00.000000
....

然后可能是一个像

那样的(时间之和)行的表
911018 AAA 2016-11-07 09:30:00.00000

希望我以一种可以理解的方式解释它。

3 个答案:

答案 0 :(得分:0)

我测试了我以前的awser并修复了一些东西。

select e.id, e.name, e.time, e.type, o.time, (o.time - e.time) test, cast((o.time - e.time) as time ) as test2
from Table1 e
left join Table1 o on o.time = (

    select min(time) from Table1 where (time > e.time) and (type=1)
)

where e.type=0

仍然错过了奇数和偶数的差距,但其接近

我没有对此进行测试,但这样的事情(可能需要调整):

select id, name, (e.time - o.time) as time 
from you_sql_table e
left join you_sql_table o ON (e.type=0) and e.time = (select min(time)
from your_sql_table where (type=1) and (time > e.time))

诀窍是在同一个表上使用select查询选择连接中的下一个值。 您可以混合主选择查询中的值。

以下示例在mysql上运行 carfuel例子:

SELECT `n`.`id`, `c`.`current` AS `previous`, `n`.`current` 
(IFNULL(n.current,0)-c.current) AS `dist`, `n`.`fuel cost` 
(IFNULL(n.current,0)-c.current)/n.`fuel cost` AS `km per euro`,
`n`.`date`, `n`.`rate` FROM `carfuel` AS `c` 
LEFT JOIN `carfuel` AS `n` ON n.current = (select min(current) 
from carfuel where current > c.current) 
WHERE ((IFNULL(n.current,0)-c.current)>0) 

id = just a record id
current = the km setting of my car at the moment i filled up my car 
fuel cost = the amount i payed to fill up my car. 
date = the date i filled up my car ( not needed ) 
rate = rate of fuel price ( not needed ) 

答案 1 :(得分:0)

我建议你在表格中创建一个新字段,使用" pairID"对于每个in,out,所以你可以按pairID分组,按时间顺序排序,并区分日期。

答案 2 :(得分:0)

据我了解你的问题,我会这样做。

with a as
(
SELECT ROW_NUMBER() OVER(ORDER BY [id] DESC) AS RowNumber,  
    [name],
    [id],
    [time],

FROM [YourTable] 
)
select * from a where RowNumber % 2 = 1

表示奇数或

select * from a where RowNumber % 2 = 0

for even。