I want to insert into a table (circuit) using a select which takes values from 2 tables (segment and wgs). My query:
INSERT INTO circuit (id_circuit, description, date_start, date_end, speed,
length, duration)
SELECT (seg.id_segment, cir.nomcircuit, seg.date_start, seg.date_end, seg.speed_average,
cir.shape_leng, (seg.date_end - seg.date_start))
FROM segment seg, wgs cir where seg.id = 13077
My Tables: circuit:
CREATE TABLE circuit
(
id serial NOT NULL,
id_circuit integer,
description character varying(50),
date_start time without time zone,
date_end time without time zone,
speed double precision,
length double precision,
duration double precision,
CONSTRAINT circuit_pkey PRIMARY KEY (id)
)
segment:
CREATE TABLE segment
(
id serial NOT NULL,
id_segment integer,
date_start timestamp without time zone,
date_end timestamp without time zone,
speed_average double precision,
mt_identity character varying,
truck_type character varying,
CONSTRAINT segment_pkey PRIMARY KEY (id)
)
wgs:
CREATE TABLE wgs
(
id serial NOT NULL,
nomcircuit character varying(50),
shape_leng numeric,
CONSTRAINT wgs_pkey PRIMARY KEY (id)
)
But when I run my query, this error comes:
ERROR: INSERT has more target columns than expressions
LINE 1: INSERT INTO circuit (id_circuit, description, dat...
^
HINT: The insertion source is a row expression containing the same number of columns
expected by the INSERT. Did you accidentally use extra parentheses?
As far I can see, I do not have extra parentheses, I double checked the columns data type and made sure they match and various tries, but I still don't get why the error comes. PS: the 13077 is just to try it out with one value I'm sure I have.
This constructs an anonymous composite value:
For example:
Note that that is a single composite value, not multiple values.
From the fine manual:
The Row Constructors section might also be of interest.
When you say this:
your
SELECT
clause only has one column as the whole(...)
expression represents a single value. The solution is to simply drop those parentheses: