Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

430
Views
Cómo encontrar la diferencia entre dos valores en la misma columna filas diferentes en mysql

Necesito crear una consulta para definir cambios en el precio de una versión diferente. Por ejemplo esta es la tabla:

 id | price | date| version 1 | 10 |2020-06-01| 1 1 | 15 |2020-06-12| 2 2 | 4 |2020-06-03| 1 2 | 5 |2020-06-04| 2 2 | 5.5 |2020-06-10| 3

Empecé a crear una consulta como esta:

 select t1.price - t2.price from product_price_version t1, product_price_version t2 where t1.version = t2.version - 1

Necesito tener como resultados:

 id | price | date| version | difference 1 | 10 |2020-06-01| 1 | 0 1 | 16 |2020-06-12| 2 | 6 2 | 4 |2020-06-03| 1 | 0 2 | 5 |2020-06-04| 2 | 1 2 | 5.5 |2020-06-10| 3 | 1.5

al final para agregar un filtro y mostrar valores donde la diferencia es mayor a 5

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puedes usar lag() :

 select t.*, price - lag(price, 1, price) over(partition by id order by version) diff from mytable t

En versiones anteriores, puede unirse a sí mismo o usar una subconsulta correlacionada:

 select t.*, t.price - coalesce(t1.price, t.price) diff from mytable t left join mytable t1 on t1.id = t.id and t1.version = t.version - 1
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!