I have the following SQL Server 2008 table:
ID Destination Last Result 01 (555) 319-5170 Disconnect 02 (555) 319-5170 Duplicate 03 (555) 319-5170 Duplicate 04 (555) 261-5000 Duplicate 05 (555) 261-5000 Duplicate 06 (555) 261-7325 Busy 07 (555) 261-7325 Duplicate 08 (555) 261-7345 No Answer 09 (555) 261-7345 Duplicate
My goal is to dynamically set the ‘Last Result’ column for all matching phone numbers. So, in the example table above for Destination((555) 319-5170), the result for all matching phone numbers(555) 319-5170) with the value ‘Disconnect’. The sample update statement is below:
UPDATE C
SET [Last Result] = (
SELECT Destination
FROM Call
WHERE [Last Result] NOT IN ('Duplicate','No Phone #')
GROUP BY Destination
HAVING COUNT(Destination) > 1)
FROM Call C
INNER JOIN
(
SELECT Destination
FROM Call
WHERE [Last Result] NOT IN ('Duplicate','No Phone #')
GROUP BY Destination
HAVING COUNT(Destination) > 1
) Dup ON Dup.Destination = C.Destination
WHERE C.[Last Result] NOT IN ('Duplicate','No Phone #')
But I am getting 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.
**EXPECTED OUTPUT**: ID Destination Last Result 01 (555) 319-5170 Disconnect 02 (555) 319-5170 Disconnect 03 (555) 319-5170 Disconnect 04 (555) 261-5000 Duplicate 05 (555) 261-5000 Duplicate 06 (555) 261-7325 Busy 07 (555) 261-7325 Busy 08 (555) 261-7345 No Answer 09 (555) 261-7345 No Answer
Thanks in advance for any assistance with a possible solution.
You can look up the first
Last Resultwith therow_number()window function. However, there has to be a way to define “first”. In the example, I’m assuming you have anidentitycolumn:Example at SQL Fiddle.
If you do not have an identity column, please update the question with how you define “first”. There could be a
CreateDtcolumn for example.