I have a table that I want to have a precalulcate length on a character field
and group and sum up. Thought I could do this by creating a view with a group
by clause that includes the sum function. Unfortunately, the compiler
complains with:
A clustered index cannot be created on the view 'MyView' because the index
key includes columns which are not in the GROUP BY clause.
Wish I could verbalize the problem a little better, but the following pared
down example should serve as a demonstration:
SET ANSI_WARNINGS ON
SET ANSI_PADDING ON
SET ANSI_NULLS ON
SET ARITHABORT ON
SET CONCAT_NULL_YIE LDS_NULL ON
SET QUOTED_IDENTIFI ER ON
SET NUMERIC_ROUNDAB ORT OFF
GO
CREATE TABLE myTable(
myID INT NOT NULL,
RecNum INT NOT NULL,
TestString VARCHAR(80) NOT NULL)
GO
INSERT INTO myTable VALUES(1, 1, 'a')
INSERT INTO myTable VALUES(1, 2, 'ab')
INSERT INTO myTable VALUES(2, 2, 'abc')
GO
CREATE VIEW dbo.MyView WITH SCHEMABINDING AS
SELECT
myID = myID,
slen = SUM(LEN(TestStr ing)),
recn = COUNT_BIG(*)
FROM dbo.myTable
GROUP BY myID
GO
CREATE UNIQUE CLUSTERED INDEX IX_MyView ON MyView(myID, slen)
-- A clustered index cannot be created on the view 'MyView' because
-- the index key includes columns which are not in the GROUP BY clause.
GO
DROP VIEW MyView
GO
DROP TABLE myTable
GO
Thanks,
Chris Rathman