Search code examples
mysqlsqlscalaapache-sparkhive

How to use one column value from specific condition in current column in SQL


I am trying to bring a specific column value to another column but from different row based on a condition.

Sample Dataset:

enter image description here

For year 2023 last_year_sales I want to use values of this_year_sales from 2022 of that specific product.

The result should look like this:

enter image description here

Can someone please help me with the query for this logic?

Also if possible scala-spark solution would be appreciated. Thanks in advance!


Solution

  • Assuming your database (be it MySQL or Hive) supports the LAG() analytic function, you can use that:

    SELECT
        Year,
        this_year_sales,
        LAG(this_year_sales) OVER (PARTITION BY Product
                                   ORDER BY Year) AS last_year_sales,
        Product
    FROM yourTable
    ORDER BY
        Product,
        Year;