我的表格包含以下列的时间条目:
我想创建一个索引视图,按日和员工对时间条目进行分组。所以我用过:
CREATE VIEW dbo.Test1
WITH SCHEMABINDING
AS
SELECT
Date, EmployeeId, SUM(Quantity), SUM(CASE State = 1 THEN Quantity ELSE NULL END) AS QuantityApproved
FROM
TimeEntries
GROUP BY
EmployeeId, Date
CREATE UNIQUE CLUSTERED INDEX IDX_V1
ON dbo.Test1 (EmployeeId, Date);
GO
但是当我尝试将其作为索引视图时,会发生错误:
Cannot create the clustered index "IDX_V1" on view "dbo.Test1" because the view references an unknown value (SUM aggregate of nullable expression). Consider referencing only non-nullable values in SUM. ISNULL() may be useful for this.
显然,对于QuantityApproved列,使用ISNULL会有所帮助。但这不是我的解决方案,因为0也可能表示同一天有2条记录(Quantity = -1和QUantity = 1)。
此外,我可以在这种情况下使用辅助列作为ABS值,但是非常方便,因为我不需要解决任何其他问题。
还有其他办法可以解决这个问题吗?