【问题标题】:Merging CSV table rows according to the first column - sqlite根据第一列合并CSV表行 - sqlite
【发布时间】:2016-07-02 06:36:38
【问题描述】:

我正在尝试将 excel 数据库迁移到 sqlite,尽管我对后者没有专业知识。

我的第一步是将一系列 CSV 文件导入 sql 数据库(我发现这很容易做到)。 这些表有一个共同的结构,即

column1: timestamp
column2: temperature
column3: humidity

第二步是根据第一列中的值(时间戳)合并导入表中的行。行中的数据可能重叠或有间隙,例如:

timestamp,temperature,humidity
04/01/2016 09:00:00, 23.1, 45.5
04/01/2016 09:15:00, 23.3, 46
...
20/01/2016 15:15:00, 25, 40

timestamp,temperature,humidity
10/01/2016 09:00:00, 23.1, 45.5
10/01/2016 09:15:00, 23.3, 46
...
30/01/2016 15:15:00, 25, 40

如何合并两个(或多个)导入的 CSV,覆盖公共数据并将间隙的空白(或 NULL)值保留到一个主 sql 表中?

谢谢, 安德烈亚

【问题讨论】:

  • 我知道如何将 .csv 文件导入到 sqlite3,但不确定第二种情况。

标签: sql excel sqlite csv


【解决方案1】:

My_Table 具有以下列:

时间 |温度 |湿度

example.csv 有以下数据:

10/01/2016 09:15:00,23.1,45.5

10/01/2016 09:11:00,22.3,41.5

10/01/2016 09:15:00,23.1,42.5

转到 Sqlite3 终端并运行以下命令。

.separator ","
.import example.csv My_Table

同样你可以导入所有的csv文件,最后你可以根据时间删除重复的行。

delete from My_Table where rowid not in (select max(rowid) from My_Table group by time);

【讨论】:

  • 非常感谢您的回复。我假设“时间”必须引用“My_Table”中的现有列?
猜你喜欢
  • 1970-01-01
  • 2019-06-20
  • 2019-11-05
  • 2019-12-29
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
  • 2012-12-21
  • 2018-08-14
相关资源
最近更新 更多