I am trying to put an index on a view but keep running into the issue where I get an error
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.
Code:
USE [DB]
GO
/****** Object: View [dbo].[MonthlyView] Script Date: 03/17/2014 15:19:10 ******/
SET ANSI_NULLS ON
GO
--Set the options to support indexed views.
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER VIEW [dbo].[MonthlyView2]
WITH SCHEMABINDING
AS
SELECT
ISNULL(SUM(I.RSQty),0) AS RSQty,
ISNULL(SUM(I.COGQty),0) AS COGQty,
ISNULL(SUM(I.DSQty),0) AS DSQty,
ISNULL(SUM(I.LossQty),0) AS LossQty,
ISNULL(SUM(I.AbuseQty),0) AS AbuseQty,
ISNULL(SUM(I.InventoryChgQty),0) AS InventoryChgQty,
ISNULL(SUM(I.RSRev),0) AS RSRev,
ISNULL(SUM(I.COGRev),0) AS COGRev,
ISNULL(SUM(I.DSRev),0) AS DSRev,
ISNULL(SUM(I.LossRev),0) AS LossRev,
ISNULL( SUM(I.AbuseRev),0) AS AbuseRev,
ISNULL(SUM(I.InventoryChgRev),0) AS InventoryChgRev,
ISNULL(SUM(I.EnergyChg + I.EnvironChg + I.HandlingChg + I.OtherChg + I.Discount + I.StopMin),0) AS Surcharge,
COUNT_BIG(*) as cbig,
M.MonthId, PeriodEnd, id, Sku
FROM dbo.ItemRev AS I INNER JOIN
dbo.Weeks AS w ON I.WeekId = w.WeekId INNER JOIN
dbo.Month AS M ON M.MonthId = w.MonthId
GROUP BY M.MonthId, PeriodEnd, id, Sku
GO
CREATE UNIQUE CLUSTERED INDEX IX_vMonthly on [dbo].[MonthlyView2]
(
[ID] asc,
[PeriodEnd] asc,
[MonthId] asc,
[SKU] asc
)
go