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 Sample_id. Operator_Id and Chart_Id comes from other tables that are beside the point.
What I need is the following: I need to have a SELECT that tells me how to return the Date, Value, and moreover a column called AMPLITUDE. AMPLITUDE is the value minus the previous value.
I'll 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 offset from the other (I did this, then I do the amplitude calculation 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 forgot to explain, but my select should only return the last 25 values - so I used LIMIT.
In this Select that 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 to use left join .
Any ideas? Thankful.