2

我正在尝试在 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]

谢谢!

4

2 回答 2

1

错误消息告诉您它不喜欢聚合函数 SUM 和 COUNT_BIG。这些是不确定的,因为 GROUP BY 返回的每个组中的行数可能在执行之间有所不同。

于 2015-03-24T14:11:50.760 回答
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
于 2015-03-24T14:10:37.307 回答