I have data in a table like
date | city | Cumulative total
---------------------------------
1/1/2020 | NYC | 10
1/2/2020 | NYC | 15
1/3/2020 | NYC | 31
1/4/2020 | NYC | 36
1/5/2020 | NYC | 55
.
. // more data for NYC continued
.
1/1/2020 | BER | 1
1/2/2020 | BER | 5
1/3/2020 | BER | 13
1/4/2020 | BER | 42
1/5/2020 | BER | 45
.
. // more data for BER continued
.
I want this data to not hold the cumulative
, but rather hold the difference
. Basically I want to subtract the next day from the day before it, making sure that the cities match up.
date | city | Cumulative total
---------------------------------
1/1/2020 | NYC | 10
1/2/2020 | NYC | 5
1/3/2020 | NYC | 16
1/4/2020 | NYC | 5
1/5/2020 | NYC | 19
.
. // more data for NYC continued
.
1/1/2020 | BER | 1
1/2/2020 | BER | 4
1/3/2020 | BER | 8
1/4/2020 | BER | 29
1/5/2020 | BER | 3
.
. // more data for BER continued
.
I have the data within a CSV and am to load it into a database for a spring boot application. However, the spring boot application needs the difference, not the cumulative. How can I properly transform this data either
Within the database upon reading the data from the CSV?
By writing a special query within the JpaRepository
so that my POJO's come back as the transformed data?
I have no idea how to implement either of the previous, but they are my ideas for what to do. I ask that someone help me see what the most "industry standard" way to handle this situation is. Maybe there is a better way than what I proposed.
Thanks!
If your database supports window functions, this is an easy task for lag()
, which lets you access any column on the previous row, given a partition
and order by
specification:
select
t.*,
cumulative
- lag(cumulative, 1, 0) over(partition by city order by date) as difference
from mytable t
Not all databases support the 3-argument form of lag()
, in which case you can do:
select
t.*,
coalesce(
cumulative - lag(cumulative) over(partition by city order by date),
cumulative
) difference
from mytable t