I added the column using the alter table function, but I am now trying to use the 'update' and 'set' syntax to successfully update the table and translate the data over in the desired format
I'm basically trying to translate this into a format I can use to update the table:
select record_no, string_agg(final_ip_description, ' | ') as Concatenated_IP
from [dm_na_fin_da].[updated_ip_dataset]
group by record_no
Example of desired output (by record_no):
record_no Concatenated_IP
--------------------------------------------------
382102878 Transportation | Energy | Construction
When I try to use the below, I get the following error:
alter table [dm_na_fin_da].[ip_tiebreaker_ref_table]
add Concatenated_IP varchar(100);
update [dm_na_fin_da].[ip_tiebreaker_ref_table]
set Concatenated_IP = (select string_agg(final_ip_description, ' | ')
from [dm_na_fin_da].[updated_ip_dataset]
group by record_no)
Error
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Is there a way for me to update this newly formed table column so it displays as shown above?
3条答案
按热度按时间cu6pst1q1#
Assuming that you want to update the new column with the list of descriptions of all IPs of each
record_no
, then, starting from your existing query, you would need to correlate the subquery to the outer query (and drop thehaving
clause):Note that this assigns the same value to all rows that have the same
record_no
(within group
is there to make the sort predictable) - which one might consider data duplication - and it is hard to maintain such information up to date when the data changes later on.Another approach would be to use a view, which gives you an always up-to-date perspective at your data for 0 maintenance costs:
67up9zun2#
The problem with your query is that your subquery is not correctly linked to the single records you're trying to update, as long as your subqueries returns multiple rows.
In order to fix this problem, you can try using the
FROM... WHERE
clauses featured in SQL ServerUPDATE
statements.v440hwme3#
You can update using
INNER JOIN
as follows :Demo here