Barry said:
Jamie, I tried it but it returned all data of [Result] summed together
without regards to [Qual] being null or not.
That is not correct.
Here is my test case:
CREATE TABLE MyTable (
[Result] INTEGER,
Qual INTEGER)
;
INSERT INTO MyTable ([Result], Qual) VALUES (5, 1)
;
INSERT INTO MyTable ([Result], Qual) VALUES (6, 1)
;
INSERT INTO MyTable ([Result], Qual) VALUES (7, 1)
;
SELECT MAX([Result]) AS max_of,
SUM([Result]) AS sum_of
FROM MyTable;
returns max_of = 7, sum_of = 18
Your statement, "[your query] returned all data of [Result] summed
together without regards to [Qual] being null or not" is a false
statement, which can be demonstrated as follows:
UPDATE MyTable SET Qual = NULL
;
SELECT IIF(EXISTS (
SELECT *
FROM MyTable AS T2
WHERE T2.Qual IS NULL
), DT1.sum_of, DT1.max_of) AS sum_or_max_of
FROM
(
SELECT MAX([Result]) AS max_of, SUM([Result]) AS sum_of
FROM MyTable
) AS DT1
;
returns sum_or_max = 18
UPDATE MyTable SET Qual = 1
;
SELECT IIF(EXISTS (
SELECT *
FROM MyTable AS T2
WHERE T2.Qual IS NULL
), DT1.sum_of, DT1.max_of) AS sum_or_max_of
FROM
(
SELECT MAX([Result]) AS max_of, SUM([Result]) AS sum_of
FROM MyTable
) AS DT1
;
returns sum_or_max = 7
The result changes based on the presence of a NULL value for Qual,
making your statement false.
My interpretation of your spec is:
If any one row of data contains a null value in [Qual] field then
return a sum of all values from [Result] field
else
return the maximum value of [Result]
I am confident my interpretation is correct. Did you word your spec
incorrectly said:
I have accidentally posted the same question in another thread (through a
different link to the newsgroup on Microsoft's website).
You seem to have posted test data to the other thread so I'll take a
look now.
Jamie.
--