2010-11-17 101 views
3

我曾嘗試使用下面的代碼錯誤添加索引到

CREATE VIEW dbo.two_weeks_performance WITH SCHEMABINDING 
AS 
    SELECT dbo.day_dim.date_time AS Date, 
     dbo.order_dim.quantity AS Target_Acheived 
    FROM dbo.day_dim 
    JOIN dbo.order_fact ON dbo.day_dim.day_id = dbo.order_fact.day_id 
    JOIN dbo.branch_dim ON dbo.order_fact.branch_id = dbo.branch_dim.branch_id 
    JOIN dbo.order_dim ON dbo.order_fact.order_id = dbo.order_dim.order_id 
GROUP BY dbo.order_dim.quantity, dbo.day_dim.date_time` 

現在創建視圖時,我使用了一個觀點:

CREATE UNIQUE CLUSTERED INDEX two_weeks_performance_I ON two_weeks_performance (Date) 

我得到一個錯誤:

Cannot create index because its select list does not use the correct usage of COUNT_BIG(). Consider adding COUNT_BIG(*) to the select.

請幫我解決這個問題。

回答

6

錯誤告訴你你要做什麼 - 將COUNT_BIG(*)添加到你的選擇列表中。

Creating Indexed Views

If GROUP BY is specified, the view select list must contain a COUNT_BIG(*) expression, and the view definition cannot specify HAVING, ROLLUP, CUBE, or GROUPING SETS.

CREATE VIEW dbo.two_weeks_performance WITH SCHEMABINDING 
AS 
    SELECT dbo.day_dim.date_time AS Date, 
     dbo.order_dim.quantity AS Target_Acheived, 
     COUNT_BIG(*) as Cnt 
    FROM dbo.day_dim 
    JOIN dbo.order_fact ON dbo.day_dim.day_id = dbo.order_fact.day_id 
    JOIN dbo.branch_dim ON dbo.order_fact.branch_id = dbo.branch_dim.branch_id 
    JOIN dbo.order_dim ON dbo.order_fact.order_id = dbo.order_dim.order_id 
GROUP BY dbo.order_dim.quantity, dbo.day_dim.date_time 
GO 
CREATE UNIQUE CLUSTERED INDEX two_weeks_performance_I ON two_weeks_performance (Date) 
+0

感謝這...這是有用的! – Deepak 2010-11-17 09:20:12