Cannot create the clustered index “%.*ls” on view “%.*ls” because the index key includes columns that are not in the GROUP BY clause. Consider eliminating columns that are not in the GROUP BY clause from the index key.

Error Message:
Msg 8661, Level 16, State 0, Line 2
Cannot create the clustered index “%.*ls” on view “%.*ls” because the index key includes columns that are not in the GROUP BY clause. Consider eliminating columns that are not in the GROUP BY clause from the index key.

Severity level:
16.

Description:
This error message appears when you try to create a clustered index on a view, but include column(s) in the index key that are not contained in the GROUP BY clause of the view.

Consequences:
The T-SQL statement can be parsed, but causes the error at runtime.

Resolution:
Errors of the Severity Level 16 are generated by the user and can be fixed by the SQL Server user. The statement cannot be executed this way. Either include the columns in the GROUP BY clause or consider not indexing the view.

Versions:
This error message was introduced with SQL Server 2000.

Example(s):
IF OBJECT_ID (‘dbo.v’, ‘View’) > 0
    DROP VIEW dbo.v;
GO

IF OBJECT_ID (‘dbo.t’) IS NOT NULL
    DROP TABLE dbo.t;
GO

CREATE TABLE dbo.t
(
    c1 int,
    c2 int
);
GO

CREATE VIEW dbo.v
WITH SCHEMABINDING
AS
SELECT
    c1, SUM(c2) AS c2, COUNT_BIG (*) AS cnt
FROM
    dbo.t
GROUP BY
    c1;
GO

CREATE UNIQUE CLUSTERED INDEX cix_v
    ON dbo.v (c1, c2);
GO

Remarks:
In the above example we try to create a clustered index on view dbo.v. Because we want to include the column c2 in the index, but this column is not part of the GROUP BY clause the error is raised.



Related Articles :

  • No Related Articles Found

Trackbacks/Pingbacks

  1. SQL Server Performance - May 22, 2011

    [...] [...]

Software Reviews | Book Reviews | FAQs | Tips | Articles | Performance Tuning | Audit | BI | Clustering | Developer | Reporting | DBA | ASP.NET Ado | Views tips | | Developer FAQs | Replication Tips | OS Tips | Misc Tips | Index Tuning Tips | Hints Tips | High Availability Tips | Hardware Tips | ETL Tips | Components Tips | Configuration Tips | App Dev Tips | OLAP Tips | Admin Tips | Software Reviews | Error | Clustering FAQs | Performance Tuning FAQs | DBA FAQs |