简体   繁体   English

将 CSV 读取到 spring 引导应用程序时,如何将数据表列从累积转换为差异?

[英]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我怎样才能正确地转换这些数据

  1. Within the database upon reading the data from the CSV?从 CSV 读取数据后在数据库内?

  2. 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()来说是一项简单的任务,它允许您访问一行的任何列,给定partitionorder 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.

相关问题 如何使用复杂的 sql 查询转换表,同时将数据从 CSV 加载到 spring 引导应用程序内的数据库中 - How to transform table using complex sql query while loading data from CSV into database within a spring boot application 如何在加载时从CSV中获取SQL LOAD DATA以遵守列类型? - How do I get SQL LOAD DATA from CSV to honour the column type when loading? 在SQL Server Management Studio中,从CSV文件中批量插入时,如何忽略表中的第一列? - In SQL Server Management Studio, how do I ignore the first column in a table when bulk inserting from a csv fle? 如何将该SQL表中的数据转换为特定格式? - How do I transform the data in this SQL Table into a specific format? 如何使用同一表中另一列的数据更新表中的列? - How do I update a column from a table with data from a another column from this same table? 将jsonb列的数据转换成postgresql中的表 - Transform data from a jsonb column into a table in postgresql 每当我重命名 spring 启动应用程序中的实体列时,如何重命名数据库中的列 - How to rename a column in database whenever I rename a column of an entity in spring boot application 如何根据列名将数据从 CSV 复制到目标表? - How can I copy data from CSV to a destination table based on column names? SQL / Python:将数据从csv转换为具有条件的不同架构的表 - SQL/Python: Transform data from csv and into table with different schema with condition 如何从一个表中选择与表中的列值相匹配的数据? - How do I select data from one table where column values from that table match the conatenated column values of another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM