我在下面的安排中给了我数据,该列下的时间戳似乎相同,但是无法以这样的方式实际导出数据,即在单个时间戳上仅给出一个时间戳柱。我该如何保持数据对的相关性?
Timestamp, pizza shop, Timestamp, clothes store, Timestamp, stadium
7/1/2019 0:00:00, 2, 7/1/2019 0:00:00, 4, 7/1/2019 0:00:00, 5
7/1/2019 0:00:10, 3, 7/1/2019 0:00:10, 6, 7/1/2019 0:00:10, 7
答案 0 :(得分:0)
您可以使用LookupAccountSid()
将csv文件作为表挂载,然后使用它来选择另一个表。
例如:
CREATE EXTENSION file_fdw;
CREATE SERVER files FOREIGN DATA WRAPPER file_fdw;
CREATE FOREIGN TABLE csvfile (
pizzashop_ts timestamp with time zone,
pizzashop_count int,
clothesstore_ts timestamp with time zone,
clothesstore_count int,
stadium_ts timestamp with time zone,
stadium_count int)
SERVER files
OPTIONS ( filename '/path/to/csv.file', format 'csv', header 'true');
然后我们从表中多次选择,但一次只选择列对,将它们与UNION ALL结合在一起,然后添加“ source”列以对数据进行分类:
SELECT pizzashop_ts AS "timestamp", pizzashop_count AS "count", 'pizza shop' AS source
FROM csvfile
UNION ALL
SELECT clothesstore_ts, clothesstore_count, 'clothes store' AS source
FROM csvfile
UNION ALL
SELECT stadium_ts, stadium_count, 'stadium' AS source
FROM csvfile;
这应该像这样布置您的CSV文件:
timestamp | count | source
------------------------+-------+---------------
2019-07-01 00:00:00+01 | 2 | pizza shop
2019-07-01 00:00:10+01 | 3 | pizza shop
2019-07-01 00:00:00+01 | 4 | clothes store
2019-07-01 00:00:10+01 | 6 | clothes store
2019-07-01 00:00:00+01 | 5 | stadium
2019-07-01 00:00:10+01 | 7 | stadium
(6 rows)
自然地,您可以将其转换为INSERT查询,以将数据插入合适的表中。
披露:我是EnterpriseDB(EDB)员工。