0

I want to update table with data in another table

Table 1 OverseasOperation and table 2 is ForecastTotal

I used this command but it copy only the first value

Update ForecastTotal 
Set OVERSEASVol = Sales 
From OverseasOperation

Result is first value in Sales = OVERSEASVol

The result is

enter image description here

but I want to get the table like this

enter image description here

enter image description here

enter image description here

What's wrong ?

Note: I want to update not insert, and don't want to make condition because I want to copy all data

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Amr Kamal
  • 137
  • 3
  • 7
  • 17

3 Answers3

0

The most generic method is to use a correlated subquery:

Update ForecastTotal
    Set OVERSEASVol = (select Sales
                       from OverseasOperation oo
                       where oo.?? = ForecastTotal.??
                      );

The ?? is for the join conditions. It is unclear what column(s) are used to connect the two tables.

Note: Many databases support some method of joining tables in the update statement. Those methods tend to depend on the database.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • i don't want to put condition so the result is .. Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated. – Amr Kamal Apr 16 '17 at 18:36
  • @AmrKamal . . . Edit your question and provide sample data and desired results. – Gordon Linoff Apr 16 '17 at 18:43
  • I Edited the question with screenshot from database and what i want – Amr Kamal Apr 16 '17 at 18:49
0

try it

;with ForecastTotal_Temp
as
(
select ForecastTotal.OVERSEASVol as OVERSEASVol, row_number() over (order by OVERSEASVol) as rownum
from ForecastTotal
)

update ForecastTotal_Temp
set OVERSEASVol = (
                    select sub.Sales 
                    from (select OverseasOperation.Sales AS Sales, row_number() over(order by Sales ) as rnum from OverseasOperation) sub 
                    where sub.rnum=ForecastTotal_Temp.rownum
                  )
Vecchiasignora
  • 1,275
  • 7
  • 6
0

Assuming the tables share a unique common key (call it ukey) then

update ForecastTotal
   set sales = OverseasOperation.Sales
from ForecastTotal
join OverseasOperation on ForecastTotal.ukey = OverseasOperation.ukey

If the OverSeasOperation has multiple occurences of the primary key from ForecastTotal, then the sales must be summed,grouped on the primary key and wiill serve as the second table on the join, shown here

update ForecastTotal
   set sales = oo.Sales
from ForecastTotal
join 
( 
    select ukey, sum(Sales) sales
    from OverseasOperation
    group by ukey
) oo
on ForecastTotal.ukey = oo.ukey

Also, if you wish to update the ForecastTotal sales by adding the sales from the OverseasOperation, change the set from

set sales = OverseasOperation.Sales

to

set sales = sales + OverseasOperation.Sales
Gene Stempel
  • 285
  • 1
  • 5