copy records from one table to another query give error sql - sql

Using the below query to copy records from one table to another, but i get error
insert into table1 (datestamp)
select datestamp
from table2
where table1.datestamp is null
I want to copy records of datestamp from table 2 to table 1 where datestamp in table 1 is null.

Is this what you mean?
insert into table1 (datestamp)
select datestamp
from table2
where table2.datestamp is null
You are referencing table1 datestamp in the where clause and this is not allowed.
Perhaps you really want an update. If so, you need a way to link the two tables:
update t1
set datestamp = t2.datestamp
from table1 t1 join
table2 t2
on t1.id = t2.id
where t1.datestamp is null

I'm assuming the tables are tied together by some unique id? We'll call that tableID.
UPDATE table1 t1, table2 t2
SET t1.datestamp = t2.datestamp
WHERE t1.datestamp IS NULL
AND t1.tableID = t2.tableID

Related

Sql query with join on table with ID not match

I have two tables.
Table 1
Id
UpdateId
Name
Table 2
Table1ID
UpdateID
Address
Each time user update, system will insert record to table1. But for table2, system only insert record when there is update in address.
Sample data
Table 1
1,1,name1
1,2,name1
1,3,name1update
1,4,name1update
1,5,name1
1,6,name2
Table 2
1,1,address
1,4,addressupdate
I want to get the result as following
1,1,name1,address
1,2,name1,address
1,3,name1update,address
1,4,name1update,addressupdate
1,5,name1,addressupdate
1,6,name2,addressupdate
How to make use of join condition to achieve as above?
You can use a correlated subquery. Here is standard syntax, but it can be easily adapted to any database:
select t1.*,
(select t2.addressid
from table2 t2
where t2.table1id = t1.id and
t2.updateid <= t1.updateid
order by t2.updateid desc
fetch first 1 row only
) as addressid
from table1 t1;
you can use left join when you want to take all columns from left table t1 even though it doesn't match with the other table with column updateid on t2 table.
select t1.id,t1.updateid,t1.name,t2.address from table1 t1
left join table2 t2
on t2.updateid= t1.updateid
you can read more about joins here

SQL - Update last occurrence with select in clause

In Oracle, I would like do an update on a table for the last occurrence based on select in list, something like :
UPDATE table t1
set t1.fieldA = 0
where t1.id in (
select t2.id, max(t2.TIMESTAMP)
from table t2
where t2.id in (1111,2222,33333)
group by t2.id
);
This query does not works, I received an error "too many values".
Any ideas?
Thanks
Your subselect has 2(too many) fields so it cant't be compared with the id.
compare the timestamp with the max timestamp.
UPDATE table t1 set t1.fieldA = 0
where t1.TIMESTAMP = (select max(t2.TIMESTAMP) from table t2 where t2.id = t1.id )
and id in (1111,2222,33333)
I believe oracle supports multiple columns IN, so you could try:
UPDATE table t1
set t1.fieldA = 0
where
(t1.id,t1.timestamp) in ( select t2.id, max(t2.TIMESTAMP) from table t2 where t2.id in (1111,2222,33333) group by t2.id );
Essentially if you're providing a subquery to the right side of the IN that has a result set with two columns, then you have to provide the names of both the columns in brackets on the left side of the IN
Incidentally, I've never liked updating based on a max value if the requirement is that only one row be updated, as two rows with the same max value will both be updated. This kind of query is better:
Update table
Set fieldA=0
Where rowid in(
select rowid from (
select rowid, row_number() over(partition by id order by timestamp desc
) as rown from table
) where rown=1)
If, however, your primary key on the table includes the time stamp as part of the compound, then there is no concern of here being two rows with the same ID/timestamp.. it's just rarely the case!

Delete from table where multiple fields match select subquery result from other table

I want to delete an entry in a table where multiple fields match the results of another select subquery which takes data from another table.
query :
DELETE FROM table1
WHERE carriedby_circuit IN (SELECT
circuit_id
FROM table2
WHERE circuit_name IN (SELECT
t.sc_prod_service_id
FROM table3 t));
So I want to modify the query in such a way that if circuit_id form table2 is present in carriedBY_circuit or in CARRIED_CIRCUIT columns of table1. it should delete the row from table1.
You can try with merge:
merge into table1 t1
using (select t2.circuit_id from table2 t2 inner join table3 t3 on t2.circuit_name = t3.sc_prod_service_id) d
on (d.circuit_id = t1.carriedby_circuit or d.circuit_id = t1.carried_circuit)
when matched then delete;
Assuming you have 3 tables and you are working with ids. Table1, table2 and table3. Your best approach is to join table2 with table3. Then query its results, to delete from table1.
Example:
DELETE FROM table1
WHERE table1.id IN(SELECT distinct id FROM tabel1 UNION SELECT ID as id FROM tabel2);
PS: The union select will probably duplicate your Id´s, this is why I propose to use distinct.

How to delete 'orphaned' records from second table

I would like to delete records from a table if the corresponding record is not present in another table.
I.e. table1 has one-to-many relationship with table2. I need to delete orphaned records from table2 where table2.id is not present in table1.
I have tried this in Access:
DELETE *
FROM t2
RIGHT JOIN t2
ON t1.id = t2.id
WHERE t1.id is NULL
but I get "Syntax error in JOIN operation". I cannot see what is wrong.
Remove the * after DELETE..
I would have solved it like this:
DELETE FROM t2
WHERE id not in (
SELECT id from t1);
Not sure if deleting with a join will work. It would need to be a LEFT JOINthough, as you want to delete all the rows in the first part of the join that is not joined with anything. Also, you are joining t2 with itself, guessing it's just a typo..
This will help:
DELETE
from t2
FROM t1
RIGHT JOIN t2
ON t1.id = t2.id
WHERE t2.id is NULL
I had the same need as #Elizabeth and I found that #Tobb's method worked...but was slow as molasses, taking about a minute to execute on my table. I found that the following sequence of SQL commands accomplishes the same result in under one second:
ALTER TABLE t2 ADD COLUMN [USED] YESNO;
UPDATE t2 SET [USED]=true WHERE id IN (SELECT id FROM t1);
DELETE FROM t2 WHERE [USED] <> true;
ALTER TABLE t2 DROP COLUMN [USED];
There is no * in DELETE statement, so change it like this:
DELETE
FROM t2
WHERE id not in (SELECT id FROM t1)

sql query distinct join

Sorry I missed and deleted earlier question on accident again.
I have a situation, I am trying to select distinct values from table 1 that are new and store them in table 2. The problem is that table has duplicates on column "name" but it does have a key column "id", but the different ids of course map to the same name.
My idea on the query would be
INSERT INTO TABLE2
(NAME, UniqueID)
SELECT DISTINCT TABLE1.NAME, TABLE1.ID
FROM TABLE1
LEFT JOIN TABLE2 ON TABLE1.ID=TABLE2.UniqueID
WHERE TABLE2.NAME IS NULL
Need help on getting the query to return my desired results, right now it still produces duplicates in table2 (on name column), which I don't want. I would want it to only append new records even if I run the query multiple times. For example if two new records were added into table1 but one has the name already in table 2, then the query would only add 1 new record to table2
just a note: I am using ms access, so it has strict syntax on single queries
EDIT:
Folliwing input I had came with this query
INSERT INTO TABLE2
(NAME, UniqueID)
SELECT TABLE1.NAME, Min(TABLE1.ID)
FROM TABLE1
LEFT JOIN TABLE2 ON TABLE1.NAME=TABLE2.NAME
WHERE TABLE2.UniqueID IS NULL
Group By TABLE1.NAME;
but these actually had to be separated to two separate wueries in access to run without a reserver error flag but now I ran into additional problem. When I run the two separate queries, it works fine the first time, but when I run it twice trying to test to see if any new records have been added to table 1, it then appends 1 record when no new records are in table 1, so it appends a blank name value and a duplicate unique id, and continually does that same process everytime I run it.
Since you're pulling both Name and ID, the distinct keyword will only pull distinct combinations of those. Two records with the same Name and different ID's is still valid.
In the case of two Names with different ID's, which would you like to be inserted?...
insert into table2 (Name, UniqueID)
select t1.Name, MIN(t1.ID)
from table1 t1
left join table2 t2 on t1.ID = t2.UniqueID
where t2.Name is null
group by t1.Name
in response to comments, I realize the Name field is what should be joined on, to prevent dupes that already exist.
insert into table2 (Name, UniqueID)
select t1.Name, MIN(t1.ID)
from table1 t1
left join table2 t2 on t1.Name = t2.Name
where t2.UniqueID is null
group by t1.Name
INSERT INTO TABLE2 (UniqueID, NAME)
SELECT min(t1.ID) as UniqueID, t1.NAME
FROM TABLE1 t1
LEFT JOIN TABLE2 t2 ON t1.ID=t2.UniqueID
WHERE t2.NAME IS NULL
group by t1.NAME