?
Solved

UPDATE Based on select statement

Posted on 2008-10-03
17
Medium Priority
?
288 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:REA_ANDREW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 2
17 Comments
 
LVL 17

Expert Comment

by:HuyBD
ID: 22632282
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

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22632286
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

0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 22632317
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
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 17

Expert Comment

by:HuyBD
ID: 22632320
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

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22632356
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

0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 22632381
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
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22632393
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

0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 22632414
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
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22632422
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

0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 22632443
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
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22632462
>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 ?!!

0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 22632512
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.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22632516
do you have "duplicate" rows with the same brickid in the table brick?
if yes, what is the primary key in the bricks table?
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 22632546
The Primary Key in the Brick Table is BrickID int autoincrement

if has no duplicates.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 22633445
then, you cannot get duplicate values for ThumbsUp ?!
did you add the brickid in the row_number() function?
, row_number() over  (ORDER BY avg_RATING DESC, brickID) rn

Open in new window

0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 22633460
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.


0
 
LVL 20

Author Closing Comment

by:REA_ANDREW
ID: 31502695
Thanks
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question