I try to define an indexed view to create full text search index on it. The view itself is created correctly:
CREATE OR ALTER VIEW dbo.my_view WITH SCHEMABINDING AS
SELECT p.id as protector_id,
p.name as protector_name,
string_agg(cast(c.name as nvarchar(max)), ', ') as crops_names,
count_big(*) as count_big
FROM dbo.protectors p
INNER JOIN dbo.protectors_crops pc on p.id = pc.protector_id
INNER JOIN dbo.crops c on pc.crop_id = c.id
GROUP BY p.id, p.name
But when I try to create an index:
CREATE UNIQUE CLUSTERED INDEX my_view_index ON dbo.my_view (protector_id)
i get an error:
[S0001][10125] Cannot create index on view "dbo.my_view" because it uses aggregate "STRING_AGG". Consider eliminating the aggregate, not indexing the view, or using alternate aggregates. For example, for AVG substitute SUM and COUNT_BIG, or for COUNT, substitute COUNT_BIG.
Documentation doesn't state anything about STRING_AGG, neither I can find any solution to replace it.
Read the documentation again.
Don't think string_agg is deterministic - so that is likely another issue. I would skip the inclusion of the name in the view to avoid the extra join and additional overhead. Is Name unique as well or is ID the only guaranteed unique row in your first table? As it stands now, you the tuple <id, name> is unique for your statement.