How do I transform a data table column from cumulative to difference when reading CSV into spring boot application?

假如想象 提交于 2021-01-29 10:17:43

问题


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

  1. Within the database upon reading the data from the CSV?

  2. 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!


回答1:


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


来源:https://stackoverflow.com/questions/62076318/how-do-i-transform-a-data-table-column-from-cumulative-to-difference-when-readin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!