SQL Server无法创建索引视图

时间:2010-11-04 14:29:49

标签: sql-server

我使用的是SQL Server 2005 SP3标准版,但无法创建索引视图(尝试在MeasurementDataID上创建聚簇索引)。我知道标准版不会在查询中使用索引视图,除非使用了noexpand查询提示。

  

错误消息 - 因为视图引用了一个未知值和可以为空的表达式的聚合

另外,为什么我需要count_big(*)??

CREATE VIEW [CPSync].[vwGetColorChars] WITH SCHEMABINDING
AS
    SELECT MC.MeasurementDataID
       , SUM(CASE WHEN CC.ColorCharID = 11 THEN [ColorCharsValue] END ) AS [Gray_a*] 
       , SUM(CASE WHEN CC.ColorCharID = 12 THEN [ColorCharsValue] END) AS [Gray_b*]
       , SUM(CASE WHEN CC.ColorCharID = 4 THEN [ColorCharsValue] END) AS [ΔF]
       , SUM(CASE WHEN CC.ColorCharID = 9 THEN [ColorCharsValue] END) As DotGain
       , SUM(CASE WHEN CC.ColorCharID = 10 THEN     [ColorCharsValue] END) As [Gray_L*]
       , SUM(CASE WHEN CC.ColorCharID = 13 THEN     [ColorCharsValue] END) As [G7NPDC]  
       , COUNT_BIG(*) AS CountBig 
FROM CPSync.Measurement 
INNER JOIN CPSync.MeasurementData ON  Measurement.MeasurementID = MeasurementData.MeasurementID
INNER JOIN CPSync.MeasuredColorChar As MC ON MC.MeasurementDataID = MeasurementData.MeasurementDataID
INNER JOIN CPSync.ColorChar AS CC ON MC.ColorCharsID =CC.ColorCharID
Group By MC.MeasurementDataID

2 个答案:

答案 0 :(得分:5)

更改

之类的行
SUM(CASE WHEN CC.ColorCharID = 11 THEN [ColorCharsValue] END ) AS [Gray_a*] 

SUM(CASE WHEN CC.ColorCharID = 11 THEN [ColorCharsValue] ELSE 0 END ) AS [Gray_a*] 

COUNT_BIG:if允许跟踪物化行。就像非unqiue聚集索引中的“uniquifier”一样。 COUNT_BIG允许大视图的32位溢出。

答案 1 :(得分:0)

或者,您可以替换它:

SELECT MC.MeasurementDataID
   , SUM(CASE WHEN CC.ColorCharID = 11 THEN [ColorCharsValue] END ) AS [Gray_a*] 
   , SUM(CASE WHEN CC.ColorCharID = 12 THEN [ColorCharsValue] END) AS [Gray_b*]
   , SUM(CASE WHEN CC.ColorCharID = 4 THEN [ColorCharsValue] END) AS [ΔF]
   , SUM(CASE WHEN CC.ColorCharID = 9 THEN [ColorCharsValue] END) As DotGain
   , SUM(CASE WHEN CC.ColorCharID = 10 THEN     [ColorCharsValue] END) As [Gray_L*]
   , SUM(CASE WHEN CC.ColorCharID = 13 THEN     [ColorCharsValue] END) As [G7NPDC]  
   , COUNT_BIG(*) AS CountBig 

使用这个更简单的代码:

SELECT MC.MeasurementDataID,
   , CC.ColorCharID
   , SUM([ColorCharsValue] ) AS [yourColumnName] 
   , COUNT_BIG(*) AS CountBig 

并在您从索引视图中进行选择时将其展开