The aggregate functions MIN
,MAX
,AVG
, and STDEV
are not allowed in an Indexed View in SQL Server (Create Indexed Views – MSDN Library).
Does SQL Server have a mechanism to index the results of the following view, even though INSERT
, UPDATE
, and DELETE
operations would become much more expensive?
SELECT
test.test_id,
MIN(samp.result) AS tMin,
MAX(samp.result) AS tMax,
AVG(samp.result) AS tAvg,
STDEV(samp.result) AS tStdev
FROM test
INNER JOIN samp ON test.test_id = samp.test_id
WHERE samp.status = 'OK'
GROUP BY test.test_id
Best Answer
Create a permanent table to house the computed results:
Create a SQL Server Agent job that performs the following actions:
Schedule this Job to run as often as you need the "view" updated.
This will allow fast DML operations, while still providing reasonably fresh info about the data.