[英]How do I transform a data table column from cumulative to difference when reading CSV into spring boot application?
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
.我希望这些数据不包含
cumulative
,而是包含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.我在 CSV 中有数据,我将其加载到 spring 引导应用程序的数据库中。 However, the spring boot application needs the difference, not the cumulative.
但是,spring 引导应用程序需要差异,而不是累积。 How can I properly transform this data either
我怎样才能正确地转换这些数据
Within the database upon reading the data from the CSV?从 CSV 读取数据后在数据库内?
By writing a special query within the JpaRepository
so that my POJO's come back as the transformed data?通过在
JpaRepository
中编写一个特殊查询,以便我的 POJO 作为转换后的数据返回?
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:如果您的数据库支持 window 函数,这对于
lag()
来说是一项简单的任务,它允许您访问前一行的任何列,给定partition
和order by
规范排序:
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:并非所有数据库都支持
lag()
的 3 参数形式,在这种情况下,您可以执行以下操作:
select
t.*,
coalesce(
cumulative - lag(cumulative) over(partition by city order by date),
cumulative
) difference
from mytable t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.