I am trying to use an expression in the SELECT statement to create an indexed view. According to the MDSN guidelines for Indexed views, expressions can be used if they are deterministic. Unfortunately, when I try to create a unique clustered index over the view scripted below, i get an error message shown below. The columns on which I am performing the expressions have been converted to the integer datatype, so there shouldn't be a problem of precision as well. Any idea why this expression is not working?
Cannot create the clustered index '..' on view '..' because the select list of the view contains an expression on result of aggregate function or grouping column. Consider removing expression on result of aggregate function or grouping column from select list.
ALTER VIEW [dbo].[Table2]
WITH SCHEMABINDING
AS
SELECT [Manufacturer]
,SUM([QAV]) / COUNT_BIG(*) AS AvgQAV
,SUM([BackOrders$]) / COUNT_BIG(*) AS AvgBackorder$
,DATEPART(year, [Date]) AS Year
,DATEPART(month, [Date]) AS Month
,[fixSBU]
,[DC Name]
,COUNT_BIG(*) AS NumRows
FROM [dbo].[Copy_IOPS2]
GROUP BY [Manufacturer]
,DATEPART(year, [Date])
,DATEPART(month, [Date])
,[fixSBU]
,[DC Name]
Thanks!
The error message is telling you that it does not like the aggregate functions SUM and COUNT_BIG. These are non-deterministic because the number of rows in each Group returned by the GROUP BY may vary between executions.