Reputation: 1900
I have two tables and I want to update one row from GROUP BY
data
This is my code:
2nd code:
UPDATE [TABLE].[dbo].[Movies]
SET [NumFrames] =
(SELECT COUNT(ImageURL) as "Count"
FROM [TABLE].[dbo].[Frames]
GROUP BY Movie_Id)
GO
1st code:
(SELECT COUNT(ImageURL) as "Count"
FROM [TABLE].[dbo].[Frames]
GROUP BY Movie_Id
1st code give me a row with numbers
Count
12
6
10
10
10
I want to insert it into Movies.NumFrames
2nd code give me an Error
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Please help
I'm sure it's simple
Upvotes: 1
Views: 8577
Reputation: 55524
You need to use a WHERE
-clause (GROUP BY
is not necessary any more).
Otherwise your sub-query returns one row for each Movie_Id
, so you have more than one row to use as source for your update.
UPDATE [TABLE].[dbo].[Movies]
SET [NumFrames] =
( SELECT COUNT(ImageURL) as "Count"
FROM [TABLE].[dbo].[Frames]
WHERE [TABLE].[dbo].[Frames].Movie_Id = [TABLE].[dbo].[Movies].Movie_Id
)
Upvotes: 5