Aster UPDATE Column Does Not Exist - sql

I am attempting to UPDATE the values of a column in one table based on the count of values in a source table. I am using Teradata Aster.
When I submit the following correlated subquery, I get an error stating the column does not exist despite verifying that it does exist.
UPDATE table2
SET column =
(
SELECT count(*)
FROM table1
WHERE table2.column = table1.column
)
I feel there is something idiosyncratic about Aster, but I'm not certain.

You could use below query for simple column update from another table.
UPDATE table1
SET col2 = table2.col2
FROM table2
WHERE table1.col1 = table2.col1;
and for aggregate function in update query you could use below query.
UPDATE table1
SET col2 = table2.col2
FROM (select col1, count(col2) col2 from table2 group by col1 ) table2
WHERE table1.col1 = table2.col1;
Both the queries works fine for me.

Related

Snowflake sql is updating all rows instead of selective rows from WHERE clause

I want to update only certain rows of table1 where source='abc'. However, it ends up updating all the rows of table1 as if WHERE clause doesn't exist. I want to update a column from another table's column value where the IDs are same between the two tables
UPDATE table1
SET col1 = table2.col2
FROM table1 JOIN table2 ON table1.ID=table2.ID
WHERE table1.source='abc'
I'm running the above query on Snowflake
The correct UPDATE syntax is:
UPDATE table1
SET col1 = table2.col2
FROM table2
WHERE table1.ID = table2.ID
AND table1.source = 'abc';

I am trying to delete duplicate records using SQL statement. I am using MS Access 2010. Here is my statement:

I am trying to delete duplicate records using SQL statement. I am using MS Access 2010. Here is my statement:
DELETE * FROM table1 where not exists (SELECT min(col1) FROM table1 GROUP BY col2, col3 );
Even though the nested Select statement returns a subset of the records of table1, when I execute the delete statement, it deletes no (0) records.
You need a correlation clause. I'm not sure what the data looks like, but this should work:
DELETE *
FROM table1
WHERE col1 <> (SELECT MIN(t1.col1)
FROM table1 as t1
WHERE t1.col2 = table1.col2 AND t1.col3 = table1.col3
);
I suspect that you are trying for:
DELETE *
FROM table1
WHERE col1 NOT IN (SELECT MIN(col1) FROM table1 GROUP BY col2, col3);
This will also work, assuming that col1 is unique across all rows (the first version will work as long as col1 is unique for any pair of col2/col3 values).

Can I UPDATE multiple records using different values in a SELECT query?

For example I have table1 with field1 and field2 and want to do something like:
UPDATE table1
SET field1, field2 = (SELECT field1, field 2 FROM tableXYZ)
WHERE field 3 = 'foobar'
or do I have to do multiple SETs, running the same SELECT query several times?
Assuming whatever database you are using supports it, you can join the tables. SO:
Update table1
set field1 = tbx.field1,
field2 = tbx.field2
from table1 join tablexyz on --some key value join
You can do a tuple assignment by putting the columns on the left hand side between parentheses.
UPDATE table1
SET (column1, column2) = (SELECT col1, col2
FROM tableXYZ
WHERE ...)
WHERE column3 = 'foobar';
The above is standard SQL, but not all DBMS support that.
Note that you have to use a WHERE clause in the sub-select to make sure that select only returns a single row (you would typically make that a co-related sub-query).

sql oracle using update

I need to take data from one table to add to another table.
I got two SQL's:
SQL1:
select * from table1;
SQL2:
select * from table2 where coloumn1 = table1.coloumn6 (number)
table1 looks like:
For better understanding we call each coloumn coloumn1, coloumn2, ... coloumn9
table2 looks like:
For better understanding we call each coloumn coloumn1, coloumn2, ... coloumn13
What should happen in text
My SQL has to take the value from table1.coloumn6 (number) - checking if this value is given in table2.coloumn1
select * from table2 where coloumn1 = table1.coloumn6 (SQL2)
If yes it should update the data from table1.coloumn2 (varchar2) to table2.coloumn4 (varchar2).
If I understand correctly, you want to update table1.column2 to the corresponding value in table2.column1 based on other conditions. Based on your description:
update table1
column2 = (select column4 from table2 t2 where t2.column1 = table1.column6)
where exists (select 1 from table2 t2 where t2.column1 = table1.column6);
MERGE is good for updating/inserting tables based on other tables. Something like this should work (would be a bit more clear with distinctly-named columns). It also accepts an optional 'WHEN NOT MATCHED THEN' clause which lets you insert new records.
MERGE INTO table2
USING (SELECT column2, column6 FROM table1) table1
ON (table2.column1 = table1.column6)
WHEN MATCHED THEN
update set column4 = column2;

SQLite. "UPDATE table1 SET column1 = (SELECT column2 FROM table2);" is not working

Why this construction is not working in SQLite:
UPDATE table1 SET column1 = (SELECT column2 FROM table2);
where column1 and column2 has the same number of rows and the same type.
Use the common column to look up the matching record:
UPDATE table1
SET column1 = (SELECT column2
FROM table2
WHERE table2.Name = table1.Name)
It is not going to work because the table1.column1 only wants one value. What you are actually doing is setting table1.column1 = all the results from table2.column2.
You will probably have to join the two tables together on a common column or key
Because nested select returns more than one row probably. You try to put all the data from table2 into 1 element in table1
Is Column1 just getting the same value all the way down the column? Otherwise, you need to do a JOIN in the update here and Set Column1 = Column2 based on the join.
You are not linking the two tables
so it is trying to put all column2 values into each table1 row.
Link the two together with a field such as
Update table1 set column1 = column2
from table2 where table1.ColumnA = tables2.ColumnA
The reason why your statement isn't working is because you're telling it to put all of the values for column2 into every column1 cell in table1. You want something more like:
UPDATE
table1
SET
table1.column1 = table2.column2
FROM
table1
INNER JOIN
table2
ON
table1.id = table2.table1_id