How to formulate an indexed SQL Server view that combines different values?

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?

+3
source share
2 answers

Impossible, apparently.

+2
source
SELECT
   ItemCollectionId,
   COUNT(DISTINCT ContributorId),
   COUNT_BIG(*) AS DummyColumn
FROM Item
GROUP BY ItemCollectionId

COUNT_BIG (*) MSDN.

"no DISTINCT", ( , ), , ( ) SELECT DISTINCT...

0

Source: https://habr.com/ru/post/1743232/


All Articles