我尝试构建索引视图但返回重复键错误,有关该怎么做的建议?视图正在分组时,我无法使用原始表的主键。
非常感谢,
乔纳森
USE [IHG_MST]
GO
SET NUMERIC_ROUNDABORT OFF
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
CREATE VIEW [dbo].[bvw_Grouped_Delphi_optimised]
WITH SCHEMABINDING
AS
SELECT
count_big(*) as tmp,
SUM(isnull(id, 0)) as ID,
Holidex_Code, Export_Date, Account, PostAs,
StatusText, BookedByID, BookedByFullName,
EventStartDate, PropertyPID, PropertyName,
MtgClassName, BookingMarketSegment,
SUM(isnull(TotalFunctionRevenue, 0)) AS TotalFunctionRevenue,
SUM(isnull(FoodRevenue, 0)) AS FoodRevenue,
SUM(isnull(BevRevenue, 0)) AS BevRevenue,
SUM(isnull(RentalRevenue, 0)) AS RentalRevenue,
SUM(isnull(ResourceRevenue, 0)) AS ResourceRevenue,
SUM(isnull(AgreedRooms, 0)) AS AgreedRooms,
SUM(isnull(TotalRevenue, 0)) AS TotalRevenue,
SUM(isnull(CurrentRooms, 0)) AS CurrentRooms,
SUM(isnull(ExpectedAttendance,0)) AS ExpectedAttendance,
SUM(isnull(TotalGuestroomRevenue,0)) AS TotalGuestroomRevenue,
CreatedDate, LeadSource, LostReason, EventType,
FunctionRoomName, ReportGrouping, BookingID, ExtractDate,
BookingAbbrev, Uploaded_By, Uploaded_On
FROM
dbo.MST_Delphi_Bookings
WHERE
(Holidex_Code IS NOT NULL)
AND (ReportGrouping <> 'Booking')
AND (LostReason <> 'Operator Entry Error')
AND (LostReason <> 'test call')
GROUP BY
Holidex_Code, Export_Date, Account, PostAs, StatusText, BookedByID,
BookedByFullName, EventStartDate, PropertyPID, PropertyName, MtgClassName,
BookingMarketSegment, CreatedDate, LeadSource, LostReason, EventType,
FunctionRoomName, ReportGrouping, BookingID, ExtractDate, BookingAbbrev,
Uploaded_By, Uploaded_On
GO
CREATE UNIQUE CLUSTERED INDEX IX_Delphi_Holidex_Code
ON [dbo].[bvw_Grouped_Delphi_optimised](Holidex_Code, Export_Date)
GO
错误:
Msg 1505,Level 16,State 1,Line 1
CREATE UNIQUE INDEX语句终止,因为找到了对象名称的重复键&quot; dbo.bvw_Grouped_Delphi_optimised&#39;和索引名称&#39; IX_Delphi_Holidex_Code&#39;。重复键值为(ASDKE,2014-03-24)。
@usr:
CREATE UNIQUE CLUSTERED INDEX IX_Delphi_Holidex_Code
ON [dbo].[bvw_Grouped_Delphi_optimised](Holidex_Code, Export_Date, Account, PostAs,
StatusText, BookedByID, BookedByFullName,
EventStartDate, PropertyPID, PropertyName,
MtgClassName, BookingMarketSegment,
SUM(isnull(TotalFunctionRevenue, 0)) AS TotalFunctionRevenue,
SUM(isnull(FoodRevenue, 0)) AS FoodRevenue,
SUM(isnull(BevRevenue, 0)) AS BevRevenue,
SUM(isnull(RentalRevenue, 0)) AS RentalRevenue,
SUM(isnull(ResourceRevenue, 0)) AS ResourceRevenue,
SUM(isnull(AgreedRooms, 0)) AS AgreedRooms,
SUM(isnull(TotalRevenue, 0)) AS TotalRevenue,
SUM(isnull(CurrentRooms, 0)) AS CurrentRooms,
SUM(isnull(ExpectedAttendance,0)) AS ExpectedAttendance,
SUM(isnull(TotalGuestroomRevenue,0)) AS TotalGuestroomRevenue,
CreatedDate, LeadSource, LostReason, EventType,
FunctionRoomName, ReportGrouping, BookingID, ExtractDate,
BookingAbbrev, Uploaded_By, Uploaded_On)
GO
答案 0 :(得分:3)
您必须创建唯一的聚簇索引。文档清楚地表明没有办法绕过它。
如果您的数据在任何密钥上都不是唯一的,这通常是数据质量问题,概念错误是一个彻头彻尾的错误。因此,我建议你重新评估这个设计。
如果您坚持这样做,必须发明/编造某些唯一键。幸运的是,所有具有分组的查询都有这样一个键:分组列。将这些列添加到视图中并在其上创建索引。
这在逻辑上是您视图的主键。对事物进行分组会导致某些事情发生。在输出中是独一无二的。