2009-12-04 97 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