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
);
Related
In SQL Server I need to select rows that have a specific value in one column, but I have to then take those records and exclude them from my results if they have specific values in 2 other tables. The records I want might not exist in the other 2 tables.
SELECT Table1.ID
FROM
Table1
WHERE
Table1.Column1 = 'A'
AND
Table1.ID NOT IN (SELECT Table2.ID FROM Table2 WHERE Table2.Column2 = 'X')
AND
Table1.ID NOT IN (SELECT Table3.ID FROM Table3 WHERE Table3.Column3 = 'Y')
I keep getting records where ID does appear in Table2 but not Table3 and vice versa. What I want to do is exclude that ID if it's in either Table2 with Column2 = 'X' or Table3 with Column3 = 'Y'
I think my logic or syntax is wrong.
Plus I know "NOT IN" can do weird things if the results of a subquery contains NULLs, so I'm not sure if there's a more straightforward way to do this.
I've been rewriting this a bunch of different ways, but I'm not getting the results I want. I keep getting no records, too few records, or too many records...argh!
Any suggestions?
You need to change to OR:
SELECT Table1.ID
FROM Table1
WHERE Table1.Column1 = 'A'
AND (
Table1.ID NOT IN (SELECT Table2.ID FROM Table2 WHERE Table2.Column2 = 'X')
OR
Table1.ID NOT IN (SELECT Table3.ID FROM Table3 WHERE Table3.Column3 = 'Y'))
or you could rewrite it to:
SELECT Table1.ID
FROM Table1
WHERE Table1.Column1 = 'A'
EXCEPT (
SELECT Table2.ID FROM Table2 WHERE Table2.Column2 = 'X'
UNION ALL
SELECT Table3.ID FROM Table3 WHERE Table3.Column3 = 'Y'
)
EDIT:
SELECT DISTINCT Table1.ID
FROM Table1
LEFT JOIN Table2
ON Table1.ID = Table2.ID
AND Table2.Column2 = 'X'
LEFT JOIN Table3
ON Table1.ID = Table3.ID
AND Table3.Column3 = 'Y'
WHERE Table1.Column1 = 'A'
AND (Table2.ID IS NULL AND Table3.ID IS NULL);
i have two tables , table1 and table2.
table1
id name city uqid
1 vikas mysore 2
table2
id uqid name status
1 1 vikas pending
1 2 Vikas processing
I have a SQL query to fetch the details of table1 joined with table2
select table1.id,
table1.name,
table1.city,
table2.status
from table1
left outer join table2
on table2.uqid = table1.uqid
and table2.id = table1.id
this will give me the result set
id name city status
1 vikas mysore processing
how can i modify the above query to not to give us the result set until the status is set to "pass" in table2 for uqid = 1 and id = 1 ?
Try the following.
select table1.id,
table1.name,
table1.city,
table2.status
from table1
left outer join table2
on table2.uqid = table1.uqid
and table2.id = table1.id
where table2.status ilike 'pass';
If by stating that you need table2's uqid=id=1, you mean that you need both the fields to have same value then use the following.
select table1.id,
table1.name,
table1.city,
table2.status
from table1
left outer join table2
on table2.uqid = table1.uqid
and table2.id = table1.id
where table2.status ilike 'pass' and table2.uqid=table2.id;
Suggestion: Try to normalize your tables
I am not sure this is the proper way or any other efficient way exists , but this will give you the desired result.
select table1.id,
table1.name,
table1.city,
table2.status
from table1
left outer join table2
on table2.uqid = table1.uqid and table2.id = table1.id
where table1.id in(select distinct id from table2 where status like 'pass'
and uqid not in(select uqid from table1))
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 )
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
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