I return positive numbers. Then I group all the fields, returning only single-line ones. You can use MINor MAXto select. Both will return the correct result.
DECLARE @Tbl TABLE (id INT, value1 INT, value2 INT, value3 FLOAT)
INSERT INTO @Tbl
VALUES
(10, -1, -20, -48.5),
(10, 1, 20, 48.5),
(10, -1, -30, -26.2),
(10, 1, 30, 26.2),
(10, 1, 27, 30.5)
SELECT
T.id ,
MIN(T.value1) ,
MIN(T.value2),
MIN(T.value3)
FROM
@Tbl T
GROUP BY
T.id ,
ABS(T.value1) ,
ABS(T.value2),
ABS(T.value3) HAVING COUNT(1) = 1
Result:
id value1 value2 value3
10 1 27 30.5
I recommend that you check it for larger data. You may have performance issues.
source
share