我正在寻找一种干净、简洁的方法来有条件地执行 SUM 操作。我知道这必须相当直截了当,但我不确定如何进行。为了证明我的意思,看看这个......
DECLARE @Test TABLE
(
LevelID int,
DataFieldID INT,
RangeID INT,
Value FLOAT
)
INSERT INTO @Test VALUES (22, 6, 117, 100)
INSERT INTO @Test VALUES (22, 6, 122, 100)
INSERT INTO @Test VALUES (22, 6, 126, 100)
INSERT INTO @Test VALUES (22, 7, 117, 100)
INSERT INTO @Test VALUES (22, 7, 122, 100)
INSERT INTO @Test VALUES (22, 7, 126, 100)
INSERT INTO @Test VALUES (23, 6, 117, 100)
INSERT INTO @Test VALUES (23, 6, 122, 100)
INSERT INTO @Test VALUES (23, 6, 126, 100)
INSERT INTO @Test VALUES (23, 7, 117, 100)
INSERT INTO @Test VALUES (23, 7, 122, 100)
INSERT INTO @Test VALUES (23, 7, 126, 100)
SELECT
LevelID,
RangeID,
SUM(Value[where DataFieldID = 6]) / SUM(Value[where DataFieldID = 7])
FROM
@Test
GROUP BY LevelID, RangeID
知道实现上述 SUM 操作的最佳方法是什么吗?