2009-12-04 75 views
1

我的计算列在我的视图中有问题。创建索引视图时出错

SELECT ColumnC, ColumnA % ColumnB AS ModuloColAColB, COUNT_BIG(*) AS cBig FROM dbo.T1 
GROUP BY ColumnC, ModuloColAColB 

查询与此类似MSDN例如: http://msdn.microsoft.com/en-us/library/ms191432.aspx

当我尝试编译视图我收到错误信息,如: “无效列名ModuloColAColB”

所以我用柱更改组名称:

SELECT ColumnC, ColumnA % ColumnB AS ModuloColAColB, 
COUNT_BIG(*) AS cBig FROM dbo.T1 
GROUP BY ColumnC, ColumnA, ColumnB 

视图编译正确,但当我尝试添加索引我收到错误:

"Cannot create the clustered index 'px_test' on view 'l' 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" 

当我删除“ColumnA%ColumnB AS ModuloColAColB”一切正常。

数据库版本:SQL Server 2005企业版。

+1

在简单的重复,当你使用 GROUP BY ColumnC会发生什么,ColumnA%ColumnB – 2009-12-04 12:04:52

+0

的观点是正确的编译,但不能创建索引 – itdebeloper 2009-12-07 21:42:13

回答

1

尝试GROUP BY ColumnC, ColumnA % ColumnB

从你的链接:

..cannot contain... An expression on a column used in the GROUP BY clause, or an expression on the results of an aggregate.

我认为这意味着您可以在GROUP BY列不模。但是,你应该能够做到在GROUP BY表达和SELECT子句

+0

是的,我发现它...从这个MSDN页面的例子不适用于SQL服务器:/ – itdebeloper 2009-12-07 21:43:27