I have a table like this
A B C D E F
00002471 Sd3a28d471 0 24.00 377.500000 1
00002471 Sd3a28d471 0 353.50 377.500000 1
00002471 Sd3a28d471 1 211.00 211.000000 1
00002471 Sd3a28e471 1 343.00 343.000000 1
00002471 Sd3a28e471 0 56.00 242.370000 1
00002471 Sd3a28e471 0 177.06 242.370000 1
00002471 Sd3a28e471 0 9.31 242.370000 1
00002471 Sd3a28f471 0 10.31 10.31 1
00002471 Sd3a28f471 1 10.31 10.31 1
By grouping the columns A, B and C, I need to check which one has the lowest value in E and update the F column to 1 for the lowest value rows and rest of the columns to 0. In case the value is the same I need to update the F column to 1 where C is 1. The output I need is below
A B C D E F
00002471 Sd3a28d471 0 24.00 377.500000 0
00002471 Sd3a28d471 0 353.50 377.500000 0
00002471 Sd3a28d471 1 211.00 211.000000 1
00002471 Sd3a28e471 1 343.00 343.000000 0
00002471 Sd3a28e471 0 56.00 242.370000 1
00002471 Sd3a28e471 0 177.06 242.370000 1
00002471 Sd3a28e471 0 9.31 242.370000 1
00002471 Sd3a28f471 0 10.31 10.31 0
00002471 Sd3a28f471 1 10.31 10.31 1
I tried this below query
UPDATE T1
SET T1.F = CASE WHEN T1.E <= T2.E THEN 1 ELSE 0 END
--select t2.*
FROM
(SELECT DISTINCT A,B,C,D,E,F FROM #SalesOrder WHERE E IS NOT NULL) T1
INNER JOIN
(SELECT DISTINCT A,B,C,D,E,F FROM #SalesOrder WHERE E IS NOT NULL) T2
ON T1.A = T2.A
AND T1.B = T2.B
--AND T1.C = T2.C
WHERE T1.C = 1 AND T2.C = 0
But this query is throws an error
Error Message
Msg 4418, Level 16, State 1, Line 265
Derived table 'T1' is not updatable because it contains aggregates, or a DISTINCT or GROUP BY clause, or PIVOT or UNPIVOT operator.
@Suresh Gajera I need to take the distinct values of A,B,C and E, after taking distinct the values it will be like this
A B C E
00002471 Sd3a28d471 0 377.500000
00002471 Sd3a28d471 1 211.000000
00002471 Sd3a28e471 0 242.370000
00002471 Sd3a28e471 1 343.000000
00002471 Sd3a28f471 0 10.31
00002471 Sd3a28f471 1 10.31
After this I need to compare the value of E based on C column, in this the second row is lowest when compare to the first row, so 2 row should be 1 and first should be 0...In the same way third and fourth row should be updated..when comes to 5th and 6th rows the value is same in this case, I need to update the F column to 1 where C is 1