从选择表2插入表1

问题描述 投票:0回答:1

我有以下两个表:

postgresp=> \d tempo
                          Table "postgres.tempo"
   Column   |          Type          | Collation | Nullable | Default 
------------+------------------------+-----------+----------+---------
 user_id    | integer                |           |          | 
 session_id | bigint                 |           |          | 
 lat        | double precision       |           |          | 
 lon        | double precision       |           |          | 
 flag       | integer                |           |          | 
 alt        | double precision       |           |          | 
 days       | double precision       |           |          | 
 gpsdate    | date                   |           |          | 
 gpstime    | time without time zone |           |          |

postgres=> \d trajectory
                        Table "postgres.trajectory"
   Column   |           Type           | Collation | Nullable | Default 
------------+--------------------------+-----------+----------+---------
 user_id    | integer                  |           |          | 
 session_id | bigint                   |           | not null | 
 timestamp  | timestamp with time zone |           | not null | 
 lat        | double precision         |           |          | 
 lon        | double precision         |           |          | 
 alt        | double precision         |           |          | 
Indexes:
    "trajectory_pkey" PRIMARY KEY, btree (session_id, "timestamp")

然后我想通过以下方式将记录从tempo插入trajectory

  1. user_id加1
  2. gpsdate中的gpstimetempo字段转换为timestamp中的trajectory字段。
  • 查询:

    INSERT INTO trajectory (user_id, session_id, timestamp, lat, lon, alt) SELECT user_id + 1 , session_id , CONCAT(gpsdate, ' ', gpstime) , lat , lon , alt FROM tempo

    错误:

    ERROR: column "timestamp" is of type timestamp with time zone but expression is of type text LINE 4: , CONCAT (gpsdate, ' ', gpstime) ^ HINT: You will need to rewrite or cast the expression.

    tempo表中的数据:

    posgres=> SELECT * FROM tempo LIMIT 10; user_id | session_id | lat | lon | flag | alt | days | gpsdate | gpstime ---------+----------------+-----------+------------+------+-----+------------------+------------+---------- 0 | 20090429005954 | 40.008048 | 116.316474 | 0 | 491 | 39932.0415972222 | 2009-04-29 | 00:59:54 0 | 20090429005954 | 40.009643 | 116.317842 | 0 | 300 | 39932.0416550926 | 2009-04-29 | 00:59:59 0 | 20090429005954 | 40.009794 | 116.318053 | 0 | 283 | 39932.041712963 | 2009-04-29 | 01:00:04 0 | 20090429005954 | 40.009646 | 116.31813 | 0 | 276 | 39932.0417708333 | 2009-04-29 | 01:00:09 0 | 20090429005954 | 40.009554 | 116.318121 | 0 | 273 | 39932.0418287037 | 2009-04-29 | 01:00:14 0 | 20090429005954 | 40.009432 | 116.318115 | 0 | 269 | 39932.0418865741 | 2009-04-29 | 01:00:19 0 | 20090429005954 | 40.0093 | 116.318127 | 0 | 266 | 39932.0419444444 | 2009-04-29 | 01:00:24 0 | 20090429005954 | 40.009155 | 116.318091 | 0 | 265 | 39932.0420023148 | 2009-04-29 | 01:00:29 0 | 20090429005954 | 40.009011 | 116.318092 | 0 | 265 | 39932.0420601852 | 2009-04-29 | 01:00:34 0 | 20090429005954 | 40.008857 | 116.31812 | 0 | 261 | 39932.0421180556 | 2009-04-29 | 01:00:39 (10 rows)

    编辑

    使用, CONCAT(gpsdate | | gpstime)也会产生以下错误:

    ERROR: operator does not exist: | time without time zone LINE 4: , CONCAT(gpsdate | | gpstime) ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

  • sql postgresql date time sql-insert
    1个回答
    1
    投票
    据我所知,您想从timestamp with time zonedate生成time。您可以使用+运算符以及其他强制转换:

    INSERT INTO trajectory (user_id, session_id, timestamp, lat, lon, alt) SELECT user_id + 1, session_id, (gpsdate + gpstime)::timestamp with time zone, lat, lon, alt FROM tempo

    © www.soinside.com 2019 - 2024. All rights reserved.