TSQL - CTE/#Temp Table - tsql

In SSMS 2016 I have created a CTE and then immediately after the statement, I delete some rows from the CTE.
WITH cte AS
(
SELECT [GroupID], [UserID]+0.5, [Value] from dbo.myTable
)
DELETE FROM cte WHERE concat(GroupID, UserID) in (select concat(GroupID, UserID) as Concat from cte group by GroupID)
However, I want to then INSERT the remaining rows into the existing table, but when I try, I get the following error: "Invalid object name 'cte'."
I suspect the issue has something to do with the way CTEs work. As I am fairly new to them, I'm not sure, but it seems like a CTE can only be referenced once immediately following the WITH AS? Is that true? Is there a way around this? How can I insert data from the CTE?
I was thinking about using a temp table somehow, but I don't know if there's really a difference.

cte's are one and done. You run one query with them and then they go away. You can use a temp table instead and that will persist for the duration of your session.
SELECT [GroupID], [UserID], [Value]
INTO #temp
from dbo.myTable
DELETE FROM #temp
WHERE concat(GroupID, UserID) in (select concat(GroupID, UserID) as Concat from #temp group by GroupID)
INSERT INTO your_table (col1, col2)
SELECT col1, col2
FROM #temp
DROP TABLE #temp
As for the high level diff's between cte's and temp tables. Temp tables are physical storage and you can index them. CTE's are named subqueries and not stored as tabled/objects.

Related

Liquibase insert select multiple rows postgres

I want to insert into table1 multiple rows from table2. The problem is that I have some fields in table1 that I want to compute, and some rows that I want to select from table2. For example something like this:
insert into table1 (id, selectField1, selectField2, constant)
values ((gen_random_uuid()), (select superField1 from table2), (select superField2 from table2), 'test');
So the logic is to select superField1 and superField2 from all the rows in the table2 and insert them into table1 with constant value test and generated uids. superField1 and superField2 should be from the same row in table2 when inserting in table1. How can I achieve something like this using liquibase?
P.S: I'm using <sql> tag since it's easier to implement using SQL than using XML changeset, but if you know how to do it in XML that would be appreciated too, but just in SQL will be enough too. DBMS is postgres.
Don't use the VALUES clause if the source is a SELECT statement:
insert into table1 (id, selectField1, selectField2, constant)
select gen_random_uuid(), superField1, superField2, 'test'
from table2;

How to use the same common table expression in two consecutive psql statements?

I'm trying to perform a pretty basic operation with a few steps:
SELECT data from table1
Use id column from my selected table to remove data from table2
Insert the selected table from step 1 into table2
I would imagine that this would work
begin;
with temp as (
select id
from table1
)
delete from table2
where id in (select id from temp);
insert into table2 (id)
select id from temp;
commit;
But I'm getting an error saying that temp is not defined during my insert step?
Only other post I found about this is this one but it didn't really answer my question.
Thoughts?
From Postgres documentation:
WITH provides a way to write auxiliary statements for use in a larger
query. These statements, which are often referred to as Common Table
Expressions or CTEs, can be thought of as defining temporary tables
that exist just for one query.
If you need a temp table for more than one query you can do instead:
begin;
create temp table temp_table as (
select id
from table1
);
delete from table2
where id in (select id from temp_table);
insert into table2 (id)
select id from temp_table;
commit;

postgress: insert rows to table with multiple records from other join tables

ّ am trying to insert multiple records got from the join table to another table user_to_property. In the user_to_property table user_to_property_id is primary, not null it is not autoincrementing. So I am trying to add user_to_property_id manually by an increment of 1.
WITH selectedData AS
( -- selection of the data that needs to be inserted
SELECT t2.user_id as userId
FROM property_lines t1
INNER JOIN user t2 ON t1.account_id = t2.account_id
)
INSERT INTO user_to_property (user_to_property_id, user_id, property_id, created_date)
VALUES ((SELECT MAX( user_to_property_id )+1 FROM user_to_property),(SELECT
selectedData.userId
FROM selectedData),3,now());
The above query gives me the below error:
ERROR: more than one row returned by a subquery used as an expression
How to insert multiple records to a table from the join of other tables? where the user_to_property table contains a unique record for the same user-id and property_id there should be only 1 record.
Typically for Insert you use either values or select. The structure values( select...) often (generally?) just causes more trouble than it worth, and it is never necessary. You can always select a constant or an expression. In this case convert to just select. For generating your ID get the max value from your table and then just add the row_number that you are inserting: (see demo)
insert into user_to_property(user_to_property_id
, user_id
, property_id
, created
)
with start_with(current_max_id) as
( select max(user_to_property_id) from user_to_property )
select current_max_id + id_incr, user_id, 3, now()
from (
select t2.user_id, row_number() over() id_incr
from property_lines t1
join users t2 on t1.account_id = t2.account_id
) js
join start_with on true;
A couple notes:
DO NOT use user for table name, or any other object name. It is a
documented reserved word by both Postgres and SQL standard (and has
been since Postgres v7.1 and the SQL 92 Standard at lest).
You really should create another column or change the column type
user_to_property_id to auto-generated. Using Max()+1, or
anything based on that idea, is a virtual guarantee you will generate
duplicate keys. Much to the amusement of users and developers alike.
What happens in an MVCC when 2 users run the query concurrently.

Issue Insert Into (BigTable1) SELECT From (BiggerTable2)

I am selecting a set of data from a bigTable1 (index-ed) and then inserting them into another bigTable2 (index-ed)
I have two options: Which is a good idea:
Option: 1
INSERT INTO bigTable2 (bigTable2.Col1, bigTable2.Col2)
SELECT bigTable1.Col1, bigTable1.Col2 FROM bigTable1 (nolock)
WHERE bigTable1.col3 between #value1 and #value2
Option: 2
CREATE #TEMP (Col1 int, Col2 varchar(200))
INSERT INTO #TEMP (Col1, Col2)
SELECT bigTable1.Col1, bigTable1.Col2 FROM bigTable1 (nolock)
WHERE bigTable1.col3 between #value1 and #value2
INSERT INTO bigTable2 (bigTable2.Col1, bigTable2.Col2)
SELECT Col1, Col2 FROM #TEMP
I do not want to lock the bigTable1. Please advise which one is the better one between the two? Is there any other suggestion?
If you don't want to lock the table, then go with the first approach. It's a one-step procedure and, no matter how long it takes, you keep the table unlocked, so you don't block others.
The second option would be appropriate in case you wanted the table locked during the select. Having to fill an empty, non-indexed table would be faster, thus keeping your table locked for a shorter period.

Insert data into table effeciently, postgresql

I am new to postgresql (and databases in general) and was hoping to get some pointers on improving the efficiency of the following statement.
I am inserting data from one table to another, and do not want to insert duplicate values. I have a rid (unique identifier in each table) that are indexed and are Primary Keys.
I am currently using the following statement:
INSERT INTO table1 SELECT * FROM table2 WHERE rid NOT IN (SELECT rid FROM table1).
As of now the table one is 200,000 records, table2 is 20,000 records. Table1 is going to keep growing (probably to around 2,000,000) and table2 will stay around 20,000 records. As of now the statement takes about 15 minutes to run. I am concerned that as Table1 grows this is going to take way to long. Any suggestions?
This should be more efficient than your current query:
INSERT INTO table1
SELECT *
FROM table2
WHERE NOT EXISTS (
SELECT 1 FROM table1 WHERE table1.rid = table2.rid
);
insert into table1
select t2.*
from
table2 t2
left join
table1 t1 on t1.rid = t2.rid
where t1.rid is null