我试图在SELECT语句中使用表达式来创建索引视图。根据MDSN索引视图指南,如果表达式是确定性的,则可以使用它们。不幸的是,当我尝试在下面编写的视图上创建一个唯一的聚簇索引时,我收到如下所示的错误消息。我正在执行表达式的列已转换为整数数据类型,因此也不应存在精度问题。 知道为什么这个表达不起作用?
无法在视图'..'上创建聚簇索引'..',因为视图的选择列表包含聚合函数或分组列结果的表达式。考虑从聚合函数的结果中删除表达式或从选择列表中分组列。
ALTER VIEW [dbo].[Table2]
WITH SCHEMABINDING
AS
SELECT [Manufacturer]
,SUM([QAV]) / COUNT_BIG(*) AS AvgQAV
,SUM([BackOrders$]) / COUNT_BIG(*) AS AvgBackorder$
,DATEPART(year, [Date]) AS Year
,DATEPART(month, [Date]) AS Month
,[fixSBU]
,[DC Name]
,COUNT_BIG(*) AS NumRows
FROM [dbo].[Copy_IOPS2]
GROUP BY [Manufacturer]
,DATEPART(year, [Date])
,DATEPART(month, [Date])
,[fixSBU]
,[DC Name]
谢谢!
答案 0 :(得分:1)
错误消息告诉您它不喜欢聚合函数SUM和COUNT_BIG。这些是不确定的,因为GROUP BY返回的每个Group中的行数可能因执行而异。
答案 1 :(得分:0)
试试这个,
ALTER VIEW [dbo].[Table2] WITH SCHEMABINDING AS
SELECT [Manufacturer] ,
SUM([QAV]),
COUNT_BIG(*),
SUM([BackOrders$]),
COUNT_BIG(*) AS AvgBackorder$ ,DATEPART(YEAR, [Date]) AS YEAR ,
DATEPART(MONTH, [Date]) AS MONTH ,
[fixSBU] ,
[DC Name] ,
COUNT_BIG(*) AS NumRows
FROM [dbo].[Copy_IOPS2]
GROUP BY [Manufacturer] ,
DATEPART(YEAR, [Date]) ,
DATEPART(MONTH, [Date]) ,
[fixSBU] ,
[DC Name]
SELECT [Manufacturer] , SUM([QAV]) / COUNT_BIG(*) AS AvgQAV,
SUM([BackOrders$]) / COUNT_BIG(*) AS AvgBackorder$, ,DATEPART(YEAR, [Date]) AS YEAR,
DATEPART(MONTH, [Date]) AS MONTH,
[fixSBU],
[DC Name] ,
COUNT_BIG(*) AS NumRows