【问题标题】:Inserting into table1 from select table2从选择表2插入表1
【发布时间】:2020-10-01 08:36:40
【问题描述】:

我有以下两张表:

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 by:

  1. user_id加1
  2. gpsdategpstime 字段从tempo 转换为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.

【问题讨论】:

  • || 而不是CONNCAT()
  • CONCAT 而不是CONNCAT只有一个N

标签: sql postgresql date time sql-insert


【解决方案1】:

据我了解,您想从datetime 生成timestamp with time zone。您可以使用 + 运算符和额外的转换来做到这一点:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 2014-03-12
    • 1970-01-01
    • 2016-05-06
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多