I'm looking for a clean, concise way to perform a SUM operation conditionally. I know this gotta be fairly straight-forward but I'm unsure of how to proceed. To demonstare what I mean, take a look at this...
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
Any idea what would be the best way to achieve the above SUM operation?