I have a query which is resulting correct values individually. I am seeing where act_in_sec <0 should result no values
and that is correct and the problem only arises when i am doing a union all with the same query but different machines. I am not sure what is going wrong.
Select Y.machine, Y.start_time, Y.end_time, TIMEDIFF(Y.end_time,Y.start_time) as act,
TIME_TO_SEC(TIMEDIFF(Y.end_time,Y.start_time)) act_in_sec
FROM
(
select X.machine, X.ts as start_time, X.next_ts as end_time
from (
select
*
, @prev AS next_ts
, @prev := ts
from
(select * from machinestatushistory where machine = 'XXX23-4-XX' order by ts desc) AA
, (select @prev:=NULL) vars where machine = 'XXX23-4-XX'
order by ts desc
) X WHERE X.MACHINE = 'XXX23-4-XX'
I am doing UNION ALL
as below.
select * from (
Select Y.machine, Y.start_time, Y.end_time, TIMEDIFF(Y.end_time,Y.start_time) as act,
TIME_TO_SEC(TIMEDIFF(Y.end_time,Y.start_time)) act_in_sec
FROM
(
select X.machine, X.ts as start_time, X.next_ts as end_time
from (
select
*
, @prev AS next_ts
, @prev := ts
from
(select * from machinestatushistory where machine = 'XXX23-4-XX' order by ts desc) AA
, (select @prev:=NULL) vars where machine = 'XXX23-4-XX'
order by ts desc
) X WHERE X.MACHINE = 'XXX23-4-XX'
)Y
UNION ALL
Select A.machine, A.start_time, A.end_time, TIMEDIFF(A.end_time,A.start_time) as act,
TIME_TO_SEC(TIMEDIFF(A.end_time,A.start_time)) act_in_sec
FROM
(
select B.machine, B.ts as start_time, B.next_ts as end_time
from (
select
*
, @prev AS next_ts
, @prev := ts
from
(select * from machinestatushistory where machine = 'XXX23-42-XX' order by ts desc) AA
, (select @prev:=NULL) vars where machine = 'XXX23-42-XX'
order by ts desc
) B WHERE B.MACHINE = 'XXX23-42-XX'
)A
)W where act_in_sec<0
I am getting wrong values only from the machine which is in the second select, no matter how i order the query.