armgon
asked on
SQL Update Statement using aggregate
I need to update a field in table pocalc from data from the same record
UPDATE POCALC
SET Pocalc.max =ceiling(SUM(slspd01+slspd 02+slspd03 +slspd04+s lspd05+sls pd06+slspd 07+slspd08 +slspd09+s lspd10+sls pd11+slspd 12)/12)
WHERE (SiteID = 'west') AND (InvtID LIKE 'fri%')
I get the following error:
An aggregate may not appear in the set list of an UPDATE statement.
I was wondering if someone could point me in the right direction to reformatting this.
UPDATE POCALC
SET Pocalc.max =ceiling(SUM(slspd01+slspd
WHERE (SiteID = 'west') AND (InvtID LIKE 'fri%')
I get the following error:
An aggregate may not appear in the set list of an UPDATE statement.
I was wondering if someone could point me in the right direction to reformatting this.
ASKER
thanks for the assistance.
unfortunately with the subquery I am getting this error:
Msg 157, Level 15, State 1, Line 3
An aggregate may not appear in the set list of an UPDATE statement.
unfortunately with the subquery I am getting this error:
Msg 157, Level 15, State 1, Line 3
An aggregate may not appear in the set list of an UPDATE statement.
UPDATE p
SET p.max = agg.Total
FROM POCALC p
INNER JOIN (
SELECT ID, ceiling(SUM(slspd01+slspd0 2+slspd03+ slspd04+sl spd05+slsp d06+slspd0 7+slspd08+ slspd09+sl spd10+slsp d11+slspd1 2)/12) ) Total
FROM POCALC p1
GROUP BY ID
) AS agg
ON p.ID = agg.ID
SET p.max = agg.Total
FROM POCALC p
INNER JOIN (
SELECT ID, ceiling(SUM(slspd01+slspd0
FROM POCALC p1
GROUP BY ID
) AS agg
ON p.ID = agg.ID
ASKER
I am getting this error:
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'FROM'.
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'FROM'.
DECLARE @max float
set @max = select ceiling(SUM(slspd01+slspd0 2+slspd03+ slspd04+sl spd05+slsp d06+slspd0 7+slspd08+ slspd09+sl spd10+slsp d11+slspd1 2)/12) FROM POCALC
UPDATE POCALC
SET Pocalc.max = @max
WHERE (SiteID = 'west') AND (InvtID LIKE 'fri%')
set @max = select ceiling(SUM(slspd01+slspd0
UPDATE POCALC
SET Pocalc.max = @max
WHERE (SiteID = 'west') AND (InvtID LIKE 'fri%')
ASKER
X-men: The following error is returned with the query
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'select'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'select'.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
UPDATE POCALC
SET Pocalc.max =
( SELECT ceiling(SUM(slspd01+slspd0
WHERE (SiteID = 'west') AND (InvtID LIKE 'fri%')