Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update column of one table with aggregate value from other table

Status
Not open for further replies.

CarrahaG

Programmer
Mar 25, 2007
98
AW
Hi

I would like to update the column of one table with an aggregate value (avg) from another table but I keep getting an error.

I run the following statements:

CREATE TABLE [dbo].[IM_ITEM]
(
[ITEM_NO] VARCHAR(20),
[AVGFACTOR] DECIMAL(15,4)
)

CREATE TABLE [dbo].[PO_RECVR_HIST_LIN]
(
[ITEM_NO] VARCHAR(20),
[COST] DECIMAL(15,4),
[RECVD_COST] DECIMAL(15,4)
)

UPDATE A
SET [AVGFACTOR] = B.[AVGFACTOR]
FROM [dbo].[IM_ITEM] A INNER JOIN

(SELECT avg(COST/RECVD_COST) AS [AVGFACTOR]
FROM [dbo].[PO_RECVR_HIST_LIN]
GROUP BY [ITEM_NO]) B

ON A.[ITEM_NO] = B.[ITEM_NO]

However, I keep getting the error below:


Error in statement # 1

Invalid column name 'ITEM_NO'
Invalid column name 'ITEM_NO'


What am I doing wrong?

Regards,
Georges
 
You forgot to add Item_No field into the derived table.

Code:
UPDATE ASET [AVGFACTOR] = B.[AVGFACTOR]
FROM [dbo].[IM_ITEM] A INNER JOIN 
(SELECT [b]ITEM_NO[/b], avg(COST/RECVD_COST) AS [AVGFACTOR] 
FROM [dbo].[PO_RECVR_HIST_LIN] GROUP BY [ITEM_NO]) B
ON A.[ITEM_NO] = B.[ITEM_NO]

PluralSight Learning Library
 

Code:
UPDATE ASET [AVGFACTOR] = B.[AVGFACTOR]
FROM [dbo].[IM_ITEM] A INNER JOIN (SELECT [COLOR=red yellow]ITEM_NO,[/color] avg(COST/RECVD_COST) AS [AVGFACTOR] FROM [dbo].[PO_RECVR_HIST_LIN] GROUP BY [ITEM_NO]) B
ON A.[ITEM_NO] = B.[ITEM_NO]

PluralSight Learning Library
 
Hello Markros

Your advice worked. Thank you.

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top