I am trying to create a insert script with postgres,
To insert into a table a maximum 1 element per person,
For example, I got this query down, but I would like to create a logic that if I already inserted a file to a person, the next file should go to the next person of that country
I’m so stucked with this situation, I’ve never seen something like this before, a script that insert just one row and then inserts to the next "person".
What is happening now is that I’m inserting 3 files to a person, and the other person from the same country doenst receive a file.
WITH get_files AS (
SELECT
type,
country
FROM files
WHERE title IS NOT NULL
)
INSERT INTO history_person_file (
name,
document_type,
age
)
SELECT
p.name,
f.type,
p.age
FROM person p
LEFT JOIN get_files f
ON p.country = f.country
ON conflict (name) do nothing;
2
Answers
Not sure but maybe this will work
Perform the restriction logic in your query. Once you have that right, insert the result into your history table:
I used a
full join
here so you can review the results before loading to your history table.