2
I have two tables: Samples and Values, which have columns as follows:
TABLE Samples
Amostra_id
Data
Operador_Id
Carta_Id
TABLE Values
Amostra_Id
Valor
As you can see, the Values table relates to the Samples table from Sampling. Operador_id and Carta_id comes from other tables that is not the case.
What I need is this: I need to have one SELECT that gives me as a return to Date, Value and in addition a column called AMPLITUDE. AMPLITUDE is the value minus the previous value.
I will try to exemplify:
VALOR l 60 l 50 l 30 l 80
AMPLITUDE l 10 l 20 l -50 l
Note: Mysql database.
I tried the following to get a column dislocated from the other one (I did this, because then I calculate the amplitude from these columns):
Select * from(
select *, curRank := @curRank + 1 as rank
from(
select * from valores
left join (amostras)
on (amostras.AMOSTRAS_ID like valores.AMOSTRAS_AMOSTRAS_ID)
order by amostras.AMOSTRAS_ID desc
) t, (select @curRank := 0) r
where t.CARTA_CARTA_ID = 2
)
left join
(
select u.valor as value2, @curRank := @curRank + 1 as rank from
(
select * from valores
left join (amostras)
on (amostras.AMOSTRAS_ID like valores.AMOSTRAS_AMOSTRAS_ID)
order by amostras.AMOSTRAS_ID desc
) u, (select @curRank :=0) r
where u.CARTA_CARTA_ID = 2
limit 1,25
) x
on (v.rank like x.rank)
I had forgotten to explain, but my select should only return the last 25 values - so I used LIMIT.
In this Select I did, it seems to be very close, but the data of the second select (value2 and rank - of the left Join) returns everything as NULL. Maybe my mistake is in using left Join.
Any idea? Grateful.
Jhon, please clarify if the amplitude was misaligned after formatting. If positive, please [Edit] to correct.
– bfavaretto
It’s okay. Thank you very much.
– Jhon