我正在尝试在视图上放置一个索引,但一直遇到我遇到错误的问题
因为视图的选择列表包含关于聚合函数或分组列的结果的表达式。考虑从聚合函数的结果中删除表达式或从选择列表中分组列。
代码:
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
答案 0 :(得分:1)
我遇到了这个错误,发现它可以使用
sum(isnull(myfieldname, 0))
而不是
isnull(sum(myfieldname), 0)
请注意,这些含义存在细微差别,因为第一个将空值视为0,但如果任何值为null,则第二个将给出零结果。
答案 1 :(得分:0)
文档列出了很多限制。你不能做什么。
一个简单的解决方法是在不执行不允许的操作的视图上创建索引,并将其包装在另一个应用“标量计算”的视图中(代码中的ISNULL
)。