I have a schema that includes the following tables (pseudo-schemes):
TABLE ItemCollection {
ItemCollectionId
...etc...
}
TABLE Item {
ItemId,
ItemCollectionId,
ContributorId
}
I need to aggregate the number of individual members in an ItemCollectionId. This is possible using a query like:
SELECT ItemCollectionId, COUNT(DISTINCT ContributorId) FROM Item
GROUP BY ItemCollectionId
I also want to pre-compute this aggregation using an indexed (materialized) view. DISTINCT prevents the index from being placed on this view. Is there a way to reformulate this that won't violate SQL Server indexed restrictions?
source
share