updating sql query value with select statement - sql

I am trying to execute a query which is something like:
update table set column=(select column1 from table1);
I just want to store the value from other table to my column
but when i try my sql query it says
ERROR 1242 (21000): Subquery returns more than 1 row
definitely this means my table1 contains more than 1 row so i want to know that is there any way to store data into column from other table with multiple row.
or basically saving content of other table as a text something like
update table set column='Data in text from other table';

You probably need a correlation clause:
update table
set column = (select column1 from table1 where table.col = table1.col);
You need to decide what column(s) are used for the correlation.

it will work as i am getting your requirement.Please let me know if your requirement is other i ll make changes in query
update table_name t1
inner join table1 t2 on t1.id =t2.id
set column =column1

Your nested query returns multiple rows so you encountered this error.
Try in this way
UPDATE FirstTable
SET FirstTable.ColumnName =tbl2.ColumnName
FROM SecondTable tbl2 WHERE tbl2.Id = FirstTable.Id
There should be a common id or something that will help to find exact row.

Related

UPDATE with join referencing same table - Redshift SQL

Assume I have a table named tab. Tab has a field called "version". Version is sequential.
In Redshift, I would like to update rows in Tab with the value from a previous row. Please tell me how to do this. Essentially:
UPDATE tab
SET tab.fieldA = tabPrior.fieldA
FROM tab tabPrior
WHERE tab.version = tabPrior.version + 1;
I would love to use a JOIN, but this does not seem to work in Redshift.
To Update with a JOIN statement you are better off writing it in a subquery.
UPDATE table
SET
col1 = TT.col1
col2 = TT.col2
FROM (SELECT T1.id, T1.col1, T2.col2
FROM T1
JOIN T2 ON T1. id ON T2. id) AS TT
WHERE table.id = TT.id
Also, I noticed that your syntax is the SET is not correct:
SET fieldA = tabPrior.fieldA
Redshift does not allow for you to pass the table when selecting the column since it goes under the assumption that you are updating 1 table at a time.
SET column = One or more columns that you want to modify. Columns that
aren't listed retain their current values. Do not include the table
name in the specification of a target column. For example, UPDATE tab
SET tab.col = 1 is invalid.
Link: https://docs.aws.amazon.com/redshift/latest/dg/r_UPDATE.html

Update all rows for one column in a table with data in another table

I appreciate any advice on this..
I have two tables where I have to update a column in my primary table with data that resides in another secondary table. I cannot rely on views, etc as this data has to be able to be edited by the user in APEX in the future. I am basically pre-populating the data for the users to reduce their manual entry.
Primary Table = Table 1
Secondary Table = Table 2
Columns to be updated in Table 1 = FTE_ID, ACCOUNT_TYPE
Columns where the data will come from Table 2 = R_ID, ACCOUNT_TYPE
Common column in both tables = TABLE1.FID AND TABLE2.FID
Here is what I have tried, but I get "single-row subquery returns more than one row" because there are multiple table1.fid rows in table1. I basically want to perform this update for ALL rows where TABLE1.FID = TABLE2.FID.
Here is my attempt:
UPDATE TABLE1
SET TABLE1.FTE_ID =
(SELECT TABLE2.R_ID FROM TABLE2 WHERE TABLE1.FID = TABLE2.FID);
Error:
single-row subquery returns more than one row
Thanks for your help,
You can fix the proximate problem by using aggregation or row number:
UPDATE TABLE1
SET TABLE1.FTE_ID = (SELECT MAX(TABLE2.R_ID)
FROM TABLE2
WHERE TABLE1.FID = TABLE2.FID
);
The subquery can only return one row; it is an "arbitrary" value from the possible matching values.
If the field is a character field and you want all matching values, then perhaps listagg is more appropriate:
UPDATE TABLE1
SET TABLE1.FTE_ID = (SELECT LISTAGG(t2.R_ID, ',') WITHIN GROUP (ORDER BY t2.R_ID)
FROM TABLE2 t2
WHERE TABLE1.FID = t2.FID
);

Select values from one table depending on referenced value in another table

I have two tables in my SQLite Database (dummy names):
Table 1: FileID F_Property1 F_Property2 ...
Table 2: PointID ForeignKey(fileid) P_Property1 P_Property2 ...
The entries in Table2 all have a foreign key column that references an entry in Table1.
I now would like to select entries from Table2 where for example F_Property1 of the referenced file in Table1 has a specific value.
I tried something naive:
select * from Table2 where fileid=(select FileID from Table1 where F_Property1 > 1)
Now this actually works..kind of. It selects a correct file id from Table1 and returns entries from Table2 with this ID. But it only uses the first returned ID. What I need it to do is basically connect the returned IDs from the inner select by OR so it returns data for all the IDs.
How can I do this? I think it is some kind of cross-table-query like what is asked here What is the proper syntax for a cross-table SQL query? but these answers contain no explaination of what they are actually doing so I'm struggeling with any implementation.
They are using JOIN statements, but wouldn't this mix entries from Table1 and Table2 together while only checking matching IDs in both tables? At least that is how I understand this http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
As you may have noticed from the style, I'm very new to using databases in general, so please forgive me if not everything is clear about what I want. Please leave a comment and I will try to improve the question if neccessary.
The = operator compares a single value against another, so it is assumed that the subquery returns only a single row.
To check whether a (column) value is in a set of values, use IN:
SELECT *
FROM Table2
WHERE fileid IN (SELECT FileID
FROM Table1
WHERE F_Property1 > 1)
The way joins work is not by "mixing" the data, but sort of combining them based on the key.
In your case (I am assuming the key field in Table 1 is unique), if you join those two tables on the primary key field, you will end up with all the entries in table2 plus all corresponding fields from table1. If you were doing this:
select * from table1, table2 where table1.fieldID=table2.foreignkey;
then, providing your key fields are set up right, you will end up with the following:
PointID ForeignKey(fileid) P_Property1 P_Property2 FileID F_Property1 F_Property2
The field values from table1 would be from matching rows.
Now, if you do this:
select table1.* from table 1, table2 where
table1.fieldID=table2.foreignkey and F_Property1>1;
Would essentially get the same set of records, but will only show the columns from the second table, and only those that satisfy the where condition for the first one.
Hope this helps :)
If I understood your question correctly this will get the job done.
Select t2.*
from table1 t1
inner join table2 t2 on t2.id = t1.id
where t1.Prop = 'SomeValue'

How to copy column values from one database to empty column in other database?

I have two databases.
Alarm
TMP
I have a table in Alarm, where in a table there is one empty column with null values.
And I have a single column table in TMP.
I want to copy this single column values to my table in Alarm database.
What I tried so far is,
update [Alarm].[dbo].[AlarmDetails] set Alarm_Message = (select * from [TMP].[dbo].[AlarmDetails$])
where 1=1
The error is
Subquery returned more than 1 value.
Please note this,
NOTE: There is no id column in source table. Only one table & one column, Alarm Message.
I know the cause of error, but how should I modify my SQL.
Thank You.
Here's an example of copying a column:
update dst
set Alarm_Message = src.AlarmMessage
from Alarm.dbo.AlarmDetails dst
join TMP.dbo.AlarmDetails src
on dst.id = src.id
You did not specify how the tables are related, so I assumed they both have an id column.
You need something like this.
update t1
set
t1.<something1> = t2.<something2>
from
[Alarm].[dbo].[AlarmDetails] t1
join [TMP].[dbo].[AlarmDetails] t2 on (t1.<cols1> = t2.<cols2>)
UPDATE results SET results.platform_to_insert = (
SELECT correct_platform
FROM build
WHERE results.BuildID=build.BuildID LIMIT 1
);

access sql appending column

I have a table that I need to Update by adding a new field.. I can alter the table and update each row . but is there a way of appending the result of a query to the table? ( I know that the result will have the same number of rows)
EDIT: So let me make it clear
I have
table1 | col1,col2
I generate another single column table
table2 | col1
I want
table3 | table1.col1,table1.col2,table2.col1
By the way table1 & table2 have no common fields so I cant join them meaningfully.
I think so. I haven't tested this, but what I can find it seems that you can use a SubQuery to do something along the lines of
UPDATE Table1 SET Column1 = Column1 & (SELECT Column2 FROM Table2 WHERE xxxx)