当创建索引视图时,出现1949和8668消息

时间:2018-07-30 23:03:16

标签: sql-server

我要创建以下视图:

CREATE VIEW vPRODUCT
WITH SCHEMABINDING
AS
    SELECT 
        PROD_ID AS PRODUCT_ID,
        YEAR(TIME_ID) AS YEAR,
        MONTH(TIME_ID) AS MONTH,
        DATEPART(WEEK, TIME_ID) AS WEEK,
        SUM(AMOUNT_SOLD) AS TOTAL_AMOUNT_SOLD,
        SUM(QUANTITY_SOLD) AS TOTAL_QUANTITY_SOLD,
        ROUND(SUM(AMOUNT_SOLD)/SUM(QUANTITY_SOLD),2) AS AVERAGE_SALE_PRICE,
        COUNT_BIG(*) AS COUNT
    FROM 
        dbo.SALES
    GROUP BY 
        PROD_ID, YEAR(TIME_ID), MONTH(TIME_ID), DATEPART(WEEK, TIME_ID);

然后创建索引视图:

CREATE UNIQUE CLUSTERED INDEX IDX_V
    ON vPRODUCT (PRODUCT_ID, YEAR, MONTH);

这将返回消息:

  

1949年消息,第16级,状态1,第15行
  无法在视图“ dbo.vPRODUCT”上创建索引。函数“ datepart”产生不确定的结果。使用确定性系统函数,或修改用户定义的函数以返回确定性结果。

因此,我尝试创建以下视图(不使用WEEK):

CREATE VIEW vPRODUCT
WITH SCHEMABINDING
AS
    SELECT      
        PROD_ID AS PRODUCT_ID,
        YEAR(TIME_ID) AS YEAR,
        MONTH(TIME_ID) AS MONTH,
        SUM(AMOUNT_SOLD) AS TOTAL_AMOUNT_SOLD,
        SUM(QUANTITY_SOLD) AS TOTAL_QUANTITY_SOLD,
        ROUND(SUM(AMOUNT_SOLD)/SUM(QUANTITY_SOLD),2) AS AVERAGE_SALE_PRICE,
        COUNT_BIG(*) AS COUNT
    FROM 
        dbo.SALES
    GROUP BY 
        PROD_ID, YEAR(TIME_ID), MONTH(TIME_ID);

然后创建索引视图:

CREATE UNIQUE CLUSTERED INDEX IDX_V
    ON vPRODUCT (PRODUCT_ID, YEAR, MONTH);

然后我收到此消息:

  

8668消息,级别16,状态0,第14行
  无法在视图'dbo.vPRODUCT'上创建聚簇索引'IDX_V',因为视图的选择列表包含聚合函数或分组列结果的表达式。考虑从选择列表中删除聚合函数或分组列结果的表达式。

有人可以指出查询出了什么问题吗?

0 个答案:

没有答案