copy and insert rows in postgresql - postgresql

hey guys I've some data in event_name with a community_code = 1 .
now I need to add rows to the same data to the table with community_code = 2. instead of adding each row, is it possible to add all the rows with different community_code. please help me out thank you.

You can insert by querying the table and replacing values as needed. For example:
insert into community_comms (id, community_id, event_name)
select id, 2, event_name
from community_comms
where community_id = 1

Related

Fast new row insertion if a value of a column depends on previous value in existing row

I have a table cusers with a primary key:
primary key(uid, lid, cnt)
And I try to insert some values into the table:
insert into cusers (uid, lid, cnt, dyn, ts)
values
(A, B, C, (
select C - cnt
from cusers
where uid = A and lid = B
order by ts desc
limit 1
), now())
on conflict do nothing
Quite often (with the possibility of 98%) a row cannot be inserted to cusers because it violates the primary key constraint, so hard select queries do not need to be executed at all. But as I can see PostgreSQL first counts the select query as a result of dyn column and only then rejects row because of uid, lid, cnt violation.
What is the best way to insert rows quickly in such situation?
Another explanation
I have a system where one row depends on another. Here is an example:
(x, x, 2, 2, <timestamp>)
(x, x, 5, 3, <timestamp>)
Two columns contain an absolute value (2 and 5) and relative value (2, 5 - 2). Each time I insert new row it should:
avoid same rows (see primary key constraint)
if new row differs, it should count a difference and put it into the dyn column (so I take the last inserted row for the user according to the timestamp and subtract values).
Another solution I've found is to use returning uid, lid, ts for inserts and get user ids which were really inserted - this is how I know they have differences from existing rows. Then I update inserted values:
update cusers
set dyn = (
select max(cnt) - min(cnt)
from (
select cnt
from cusers
where uid = A and lid = B
order by ts desc
limit 2) Table
)
where uid = A and lid = B and ts = TS
But it is not a fast approach either, as it seeks all over the ts column to find the two last inserted rows for each user. I need a fast insert query as I insert millions of rows at a time (but I do not write duplicates).
What the solution can be? May be I need a new index for this? Thanks in advance.

postgresql "insert ... on conflict ... update" how to specify which row to update from

I have no idea how to make this work, hope you can help me
I have a table "student_to_course_term" containing the columns "course_term_id", "student_id", "student_mc_worksheet_id" and "id"
What I want to achieve is to copy all rows with course_term_id=19 to new rows with course_term_id=23. If row already exists, update the "student_mc_worksheet_id".
This is my code so far, but I'm not sure how to access the row with course_term_id=19 and course_term_id=23 after the "on conflict do update set" part. Any help appreciated!
INSERT INTO public.student_to_course_term
(course_term_id, student_id, student_mc_worksheet_id)
SELECT 23, student_id, student_mc_worksheet_id
FROM public.student_to_course_term
WHERE course_term_id = 19
ON CONFLICT ON CONSTRAINT student_to_course_term_student_id_course_term_id_key
DO UPDATE
SET student_mc_worksheet_id ( =>from the student_mc_worksheet_id=23 row) = student_mc_worksheet_id ( => from the student_mc_worksheet_id=19 row) ;

Update from two differents table with postgresql

I want to update a table with postgresql.
In fact, I have a table (TABLE_ONE) with two column (old_id and new_id). I have a second table (TABLE_TWO) with colums (id,column1,column2,...).
I want to update the column id from TABLE_TWO. The wanted behavior is that when TABLE_ONE.id = TABLE_TWO.old_id, we set id to new_id.
How can i do that?
You want an UPDATE FROM statement:
UPDATE table_one
SET table_one.id = table_two.id
FROM table_two
WHERE table_one.id = table_two.old_id;

SQL VIEW - add column based on value of another

I would like to create a view that selects ALL columns in a table, then creates/adds an additional "columnX" based on the value of another column in that same table.
For example if [column1] IS NULL, I would like [columnX] = 0 (zero)
and if [column1] IS NOT NULL, I would like [columnX] = [column1]
I think this should be simple, but I cannot seem to find the solution.
Can anyone please help?
Thanks!
case when col1 is null then 0 else col1 end as COLUMNX

Iterating through a TVP before inserting records?

I'd like some help writing the following sproc:
I have SQL Server 2008 sproc that accepts two integer values (#ID1 and #ID2) and a data table/TVP.
The TVP table contains several fields, ie. Title and Description.
I want to iterate through the TVP table and check if the Title or Description already exists in my data table, tbl_Items, where #ID1 = tbl_Items.ID1 and #ID2 = tbl_Items.ID2.
If neither exist then insert the values of #ID1 and ID2 and that TVP row into tbl_Items.
Thanks.
Something like this?
INSERT INTO tbl_Items (ID1, ID2, Title, Description)
SELECT
#ID1, #ID2, TVP.Title, TVP.Description
FROM
#TVP AS TVP
WHERE
NOT EXISTS (SELECT * FROM tbl_Items AS I WHERE TVP.Title = I.Title AND TVP.Description = I.Description)
The requirement seems somewhat unclear but you should be able to use MERGE
;WITH Target As
(
SELECT *
FROM tbl_Items
WHERE ID1=#ID1 AND ID2=#ID2
)
MERGE
INTO Target
USING #TVP AS Source
ON Target.Title = Source.Title OR Target.Description = Source.Description
WHEN NOT MATCHED
THEN INSERT (ID1, ID2, Title, Description)
VALUES (#ID1, #ID2, Title, Description)