Link to home
Start Free TrialLog in
Avatar of REA_ANDREW
REA_ANDREWFlag for United Kingdom of Great Britain and Northern Ireland

asked on

UPDATE Based on select statement

Hi, I am trying to update a value of a table from a sub query, and I am not sure where I am going wrong.  Below is what I am trying to do, and the error message which I got

Msg 116, Level 16, State 1, Line 1
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
with BrickRatings
as
(
select (SELECT AVG(RATING) FROM Rating WHERE BrickID = B.BrickID) AS Rating, * FROM Brick B 
)
 
UPDATE Brick SET ThumbsUp = 1500 - (SELECT ROW_NUMBER() OVER (ORDER BY RATING DESC), * FROM BrickRatings WHERE BrickID = Brick.BrickID)

Open in new window

Avatar of HuyBD
HuyBD
Flag of Viet Nam image

try this
with BrickRatings
as
(
select (SELECT AVG(RATING) FROM Rating WHERE BrickID = B.BrickID) AS Rating, * FROM Brick B 
)
 
UPDATE Brick SET ThumbsUp = 1500 - (SELECT ROW_NUMBER() OVER (ORDER BY RATING DESC), BrickRatings.* FROM BrickRatings WHERE BrickID = Brick.BrickID)

Open in new window

Avatar of Guy Hengel [angelIII / a3]
what about this:
with BrickRatings
as
(
  SELECT BrickID, AVG(RATING) FROM Rating WHERE BrickID GROUP BY BrickID
)
 
UPDATE Brick 
  SET ThumbsUp = 1500 - ROW_NUMBER() OVER (ORDER BY RATING DESC)
FROM Brick
JOIN BrickRatings br
  ON br.BrickID = Brick.BrickID

Open in new window

Avatar of REA_ANDREW

ASKER

HuyBD:
The error from your example was as follows:

Msg 116, Level 16, State 1, Line 1
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

angelIII:
The error from your example was as follows:

Msg 4145, Level 15, State 1, Line 4
An expression of non-boolean type specified in a context where a condition is expected, near 'GROUP'.

Thanks for your help so far
sorry
with BrickRatings
as
(
select (SELECT AVG(RATING) FROM Rating WHERE BrickID = B.BrickID) AS Rating, * FROM Brick B 
)
 
UPDATE Brick SET ThumbsUp = 1500 - (SELECT ROW_NUMBER() OVER (ORDER BY RATING DESC) FROM BrickRatings WHERE BrickID = Brick.BrickID)

Open in new window

sorry.
with BrickRatings
as
(
  SELECT BrickID, AVG(RATING) FROM Rating GROUP BY BrickID
)
 
UPDATE Brick 
  SET ThumbsUp = 1500 - ROW_NUMBER() OVER (ORDER BY RATING DESC)
FROM Brick
JOIN BrickRatings br
  ON br.BrickID = Brick.BrickID

Open in new window

HuyBD:

Each row had equal ThumbsUp of 1499

angelIII:
The error from your example was as follows:

Msg 8155, Level 16, State 2, Line 1
No column was specified for column 2 of 'BrickRatings'.

I Changed it to apply a column name and got this error

Msg 4108, Level 15, State 1, Line 1
Windowed functions can only appear in the SELECT or ORDER BY clauses.

Thanks again for the help
I see:
with BrickRatings as (
  SELECT BrickID, AVG(RATING) avg_rating FROM Rating GROUP BY BrickID
)
, br as ( 
  SELECT BrickID, avg_rating
    , row_number() over  (ORDER BY avg_RATING DESC) rn
  FROM BrickRatings
)
 
UPDATE Brick 
  SET ThumbsUp = 1500 - rn
FROM Brick
JOIN br
  ON br.BrickID = Brick.BrickID

Open in new window

AngleIII that is nearly there.  When I do a select with AVERAGE Rating, and include Row Number,  Even though the AVG rating is the same, the Row Number differs.  i.e. is sequential,  what I need is for each thumb up to be 1500 - the records row number, so giving them all a different thumbs up.  Yours is very close, but assigns a large chuk of records the same number of thumbs up.

Thanks for your time

Andrew
ok:
with BrickRatings as (
  SELECT BrickID, AVG(RATING) avg_rating FROM Rating GROUP BY BrickID
)
, br as ( 
  SELECT BrickID, avg_rating
    , row_number() over  (ORDER BY avg_RATING DESC, brickID) rn
  FROM BrickRatings
)
 
UPDATE Brick 
  SET ThumbsUp = 1500 - rn
FROM Brick
JOIN br
  ON br.BrickID = Brick.BrickID

Open in new window

Using your second example, only 720 rows are afftected and not the entire 1499 rows. Also there is still a large clump of data with the same thumbs up.

Cheers

Andrew
>Using your second example, only 720 rows are afftected and not the entire 1499 rows.
that means that not all BRICKID values from Brick table are in the Rating table ?!!

ah right, ok, how about why a large number of of the thumbs up would be the same?

Could we identify those not in there using a left join perhaps instead of an inner JOIN?  as each should have a different thumb up.
do you have "duplicate" rows with the same brickid in the table brick?
if yes, what is the primary key in the bricks table?
The Primary Key in the Brick Table is BrickID int autoincrement

if has no duplicates.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I changed JOIN to LEFT JOIN in the Update statement and worked well enough for my needs.  On that note thank you for your time.  I appreciate it!  I will assign you points now.


Thanks