按其他字段分组的最接近零的值

Value Closest to Zero Grouped by Other Fields

我需要编写一个查询,为我提供最接近零的字段值。我发现此论坛上的其他提交没有考虑 table 中的其他字段。我需要的是在其他字段上分组的最接近零的值,以便每个数据分组都有一个最接近零的值。这意味着我可以拥有 20 行数据,但最接近零的值基于其他字段。我显示的查询示例基于我在此处的论坛中找到的内容:How to find the value closest to zero using SQL Server。我更改了 WHERE 子句,以便我可以根据其他字段进行分组。

SELECT
Field1,
Field2,
Field3,
Value_I_Need_Closest_to_0

FROM Table

WHERE exists(select Field1
                  ,Field2
                  ,Field3
                  ,min(abs(Value_I_Need_Closest_to_0))
            from Table
            group by Field1
                  ,Field2
                  ,Field3)

如果我理解正确的话,你正在寻找:

select *
, min(abs(Value_I_Need_Closest_to_0)) over(partition by field1,field2, field3)
FROM tab