Basically, I have a table that stores column names with some restrictions: infos
, and another one that stores the values for those columns: info_data
. I want to get a table which has the columns from infos
and data from info_data
. I've tried with crosstab function but it doesn't have the desired effect.
I have 2 tables:
CREATE TABLE infos
(id serial PRIMARY KEY,
name text NOT NULL,
id_member integer NOT NULL,
title text,
min_length integer NOT NULL DEFAULT 0,
max_length integer NOT NULL DEFAULT 30,
required boolean NOT NULL DEFAULT false,
type text NOT NULL DEFAULT 'text'::text
);
CREATE INDEX info_id_idx ON infos (id);
and
CREATE TABLE info_data
(id serial PRIMARY KEY,
id_info integer,
value text,
CONSTRAINT info_data_id_info_fkey FOREIGN KEY (id_info)
REFERENCES infos (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);
CREATE INDEX info_data_id_idx ON info_data(id);
with the following values:
infos:
COPY infos (id, name, id_member, title, min_length, max_length, required, type) FROM stdin;
1 nume 1 Nume 0 30 t text
2 prenume 1 Prenume 0 30 t text
3 cnp 1 C.N.P. 13 13 t number
4 nume anterior 1 Nume anterior 0 30 f text
5 stare civila 1 Starea civila 0 30 f text
6 cetatenie 1 Cetatenie 0 30 f text
7 rezidenta 1 Rezidenta 0 30 f text
9 tip act 1 C.I. / B.I. 0 10 t text
10 serie ci 1 Serie C.I. / B.I. 0 30 t text
11 numar ci 1 Numar C.I. / B.I. 0 30 t text
12 data eliberarii 1 Data eliberarii 0 30 t text
13 eliberat de 1 Eliberat de 0 30 t text
8 adresa 1 Adresa 0 50 f text
\.
info_data:
COPY info_data (id, id_info, value) FROM stdin;
1 1 a
2 2 a
3 3 100
4 4
5 5
6 6
7 7
8 8
9 9 ci
10 10 sv
11 11 13
12 12 132
13 13 123
14 1 b
15 2 b
16 3 100
17 4
18 5
19 6
20 7
21 8
22 9 BI
23 10 XT
24 11 123
25 12 10
26 13 10
\.
The question:
How can I achieve this output? (the columns have to be generated based upon the unique entries from the infos
table
nume, prenume, cnp, nume anterior, ... (as columns - built from infos)
a , a, ...
b , b, ... (as rows - built from info_data)
infos
(plural), yet the sequence is namedinfo_id_seq
(singular) and the pkinfo_pkey
? I assumed typos and simplified your question accordingly. – Jubal