Deleting the records with condtion - sql

I am using SQL Server 2000
Table1
ID Date
001 23/02/2009
001 24/02/2009
002 25/02/2009
....
Table2
ID Date
001 23/02/2009
002 25/02/2009
Query Like
Delete from table1 where table1.id = table2.id and table1.date = table2.date
How to make a Query for the above condition

delete table1
from table2
where table1.id = table2.id and
table1.date = table2.date
delete table1 makes table1 the target for deletion. You could write delete from table1 but from is optional.
from table2 specifies the source for deletion. To use a "second" from clause is a t-sql extension that is used to match corresponding rows.
Finally the where clause joins the tables on columns id and date causing the matching rows in table1 to be deleted.
where table1.id = table2.id and
table1.date = table2.date

I'd do
DELETE FROM TABLE1
WHERE ID IN (SELECT t1.ID
FROM TABLE1 t1
INNER JOIN TABLE2 t2 ON t1.ID = t2.ID AND t1.Date = t2.Date)
This way you can run the select statement separately to see the data it will be deleting

Using exists usually is better (faster):
delete t1
from table1 t1
where exists (select 1 from table2 t2 where t1.id = t2.id and t1.date = t2.date)

You can also join the tables together for the delete:
DELETE FROM t1
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.id = t2.id
AND t1.Date = t2.Date

It can be done in below ways:
a. Using Join Operation
DELETE FROM Table1
FROM Table1 INNER JOIN
Table2 ON Table1.Date = Table2.Date AND Table1.Id = Table2.ID
b. Using WHERE clause
DELETE TABLE1
FROM TABLE2
WHERE Table1.Date = Table2.Date AND Table1.Id = Table2.ID

DELETE FROM table1
WHERE
( SELECT table1.id, table2.id
FROM table1, table2
WHERE table1.id = table2.id and table1.date = table2.date )

Related

Teradata update with left join and inner join

I need to bring the below sample code from mssql to Teradata. Please let me know how to convert it.Sample code -
Update table1
set table1.name = table3.name
from table1
inner join table2
on table2.id = table1.id
left join table3
on table2.id = table3.id where table3.name is null
It's ugly, but this should work. You can get around Teradata not allowing outer joins in an update by using a derived table.
update table1
from table1,
(select <column list> from table2 left join table3 on table2.id = table3.id) t
set ...
where
table1.id = t.id
and t.name is null
Like the others mentioned, you should check your NULL condition. Nevertheless, here's one more option:
Update table1
FROM (
select table1.id, table3.name
from table1
inner join table2
on table2.id = table1.id
left join table3
on table2.id = table3.id where table3.name is null
) src
SET table1.name = src.name
WHERE table1.id = src.id
You just move your original source query into a "src" sub-query and update from it. Just make sure in your "src" query you only have one row for each table1.id value so you don't get target row updated by multiple source rows errors.
I think your logic is better handled by not exists:
Update table1
set table1.name = null
where not exists (select 1
from table2 join
table3
on table2.id = table3.id
where table2.id = table1.id
);
This is not exactly what your query specifies -- this will update table1.name when there is no match in table2. If that is an issue, you can do:
update table1
set table1.name = null
where exists (select 1
from table2
where table2.id = table3.id
) and
not exists (select 1
from table2 join
table3
on table2.id = table3.id
where table2.id = table1.id
);

SQL query with multiple tables?

I'm looking to get information from one table, use that to then get data from a 2nd table and then reference the data from the 2nd table to get all relevant fields. i.e:
Table1:
ID
--
69
Table2:
entity_id | id
----------------
69 | 134
Table3:
id | postcode | cost
----------------------
134 | m21 6fh | 850
I need to do this for each entry in table1. I have been trying to use the LEFT JOIN which works until I hit the third table.
Does this not work?
SELECT *
FROM table1
LEFT JOIN table2 ON table1.ID = table2.entity_id
LEFT JOIN table3 ON table2.ID = table3.ID
Note: you will only get table3 data if you have table2 data, which is unavoidable.
Try this:
SELECT t1.*, t2.*, t3.* FROM Table1 t1
LEFT JOIN Table2 t2 ON t2.entity_id = t1.id
LEFT JOIN table3 t3 ON t3.id = t2.id
Try following
Select t1.*, t2.*, t3.* from table1
left join table2 on t1.id = t2.id
left join table3 on t1.id = t3.id -- change to t2.id = t3.id if you want to fetch only when id exists in table2
Note, result will alter if you will use inner join at any place, or use t2.id = t3.id instead of t1.id = t3.id to join on third table.
Try simply in this way:
SELECT Table3.* FROM Table1, Table2, Table3
WHERE table1.ID = table2.EntityId
AND table3.id = table2.id

sql - double condition check in sql

I have 2 tables, call Table1 and Table2. Table1 columns are id,name,col3.
Table2 has columns id, name , col3.
I want to extract all records from Table1 who's id and name (Both to satisfy condition)
not in Table2.
How to do that
Does the below assist:
SELECT
Table1.ID,
Table1.name,
Table1.col3,
Table2.ID
FROM
Table2 RIGHT JOIN Table1 ON (Table2.name = Table1.name) AND (Table2.ID = Table1.ID)
WHERE
Table2.ID Is Null;
How about this...
SELECT t1.*
FROM Table1 t1
LEFT OUTER JOIN Table2 t2
ON t1.id = t2.id AND t1.name = t2.name
WHERE t2.id IS NULL
I think you require values( id, name) which are not present in other table.
SELECT t1.*
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.id <> t2.id AND t1.name <> t2.name
WHERE t2.id is null
UNION
SELECT t2.*
FROM Table2 t2
LEFT JOIN Table1 t1
ON t1.id <> t2.id AND t1.name <> t2.name
WHERE t1.id is null

Column not found error with simple SQL query

I know its almost there, but base is telling me it can't find a column called table1.id when I know its there!
UPDATE table2 SET col1 = (SELECT field1 FROM table1 WHERE table2.id = table1.id) WHERE table1.id = table2.id
UPDATE table2 SET col1 = (SELECT field1 FROM table1 WHERE table2.id = table1.id)
table1 is unknown in the outer SQL.
What I get from your query, this will work
UPDATE table2 SET col1 = t1.field1
FROM table2 t2 INNER JOIN table1 t1 ON t2.id = t1.id
Instead of using a WHERE clause, try using an INNER JOIN clause. It is indeed late so forgive me for my code haha
UPDATE table2
SET col1 = (SELECT field1
FROM table1
WHERE table2.id = table1.id)
INNER JOIN table1
ON table2.id = table1.id
Option 1: No need to have outer WHERE clause.
Option 2: Do not use inner query unneccesarily. Use Inner Join instead

T-SQL for merging data from one table to another

Let's say there are 2 tables Table1 { ID, Name, Other } and Table2 { ID, Name, Other }. Both of them have identical records with the same IDs except that in Table1 all Name values are NULL. How can I import Name values from Table2 to Table1 using T-SQL (SQL Server 2008)?
Update Table1
Set Table1.Name = Table2.Name
From
Table1 INNER JOIN Table2 on Table1.ID = Table2.ID
You're looking for the MERGE command, which is like the UPSERT that you've probably read about elsewhere. Here's a quick article about it.
UPDATE Table1
SET Table1.Name = Table2.Name
FROM Table2
WHERE Table1.Id = Table2.Id
--AND Table1.Name IS NULL
Just join the tables and update:
update t1
set [Name] = t2.Name
from Table1 t1
inner join Table2 t2 on t2.ID = t1.ID