SQL Server 索引视图错误
我意识到这是一个非常人为的示例,但我已将完整版本简化为以下内容,以演示问题:
CREATE VIEW model.Appointments_Partition1
WITH SCHEMABINDING AS
SELECT CONVERT(varchar(15), AppointmentId) as Id,
ap.AppTypeId as AppointmentTypeId,
ap.Duration as DurationMinutes,
ap.AppointmentId as EncounterId,
COUNT_BIG(*) as __count_big
FROM dbo.Appointments ap
JOIN dbo.PracticeCodeTable pct ON SUBSTRING(pct.Code, 1, 1) = ap.ScheduleStatus
AND pct.ReferenceType = 'AppointmentStatus'
WHERE ap.AppTime > 0
GROUP BY CONVERT(varchar(15), AppointmentId), ap.AppTypeId, ap.Duration, ap.AppointmentId
CREATE UNIQUE CLUSTERED INDEX [IX_Appointments_Partition1_Id]
ON model.Appointments_Partition1 ([Id]);
我得到:
消息 8668,级别 16,状态 0,第 12 行
无法在视图“PracticeRepository.model.Appointments_Partition1”上创建聚集索引“IX_Appointments_Partition1_Id”,因为视图的选择列表包含聚合函数或分组列结果的表达式。考虑从选择列表中删除聚合函数或分组列结果的表达式。
我包括 count_big...那么为什么分组有问题?...以及如何解决该错误?
I realize this is a very contrived example, but I've simplified the full version down to the following which demonstrates the problem:
CREATE VIEW model.Appointments_Partition1
WITH SCHEMABINDING AS
SELECT CONVERT(varchar(15), AppointmentId) as Id,
ap.AppTypeId as AppointmentTypeId,
ap.Duration as DurationMinutes,
ap.AppointmentId as EncounterId,
COUNT_BIG(*) as __count_big
FROM dbo.Appointments ap
JOIN dbo.PracticeCodeTable pct ON SUBSTRING(pct.Code, 1, 1) = ap.ScheduleStatus
AND pct.ReferenceType = 'AppointmentStatus'
WHERE ap.AppTime > 0
GROUP BY CONVERT(varchar(15), AppointmentId), ap.AppTypeId, ap.Duration, ap.AppointmentId
CREATE UNIQUE CLUSTERED INDEX [IX_Appointments_Partition1_Id]
ON model.Appointments_Partition1 ([Id]);
I get:
Msg 8668, Level 16, State 0, Line 12
Cannot create the clustered index 'IX_Appointments_Partition1_Id' on view 'PracticeRepository.model.Appointments_Partition1' 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.
I'm including count_big...so why is the group by a problem?....and how can I resolve the error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是相同的错误消息,其中应用了一些布尔逻辑:
您需要删除
CONVERT(varchar(15), AppointmentId)
中的CONVERT
Here is the same error message with some boolean logic applied to it:
You need to remove the
CONVERT
inCONVERT(varchar(15), AppointmentId)
我在其中一个博客上找到这个原因,对我来说似乎很合理
http://www.tek-tips.com/viewthread.cfm?qid=1401646
您可以浏览该博客,看看它是否完全符合您的场景。
http://technet.microsoft.com/en-us/library/cc917715.aspx
如果你想在视图上建立索引,那么你必须创建带有模式绑定的视图,在上面的链接中有详细的解释。浏览设计注意事项部分
I find this reason on one of the blogs, seems reasonable to me
http://www.tek-tips.com/viewthread.cfm?qid=1401646
You can go through the blog and see if it exactly matched your scenario.
http://technet.microsoft.com/en-us/library/cc917715.aspx
If you want to build index on views, then you must create views with schema binding, in the above link it is explained in detail. Go through the section of Design Considerations