简体   繁体   中英

How to Create an indexed view in SQL Server

I'm trying to build an indexed view but returning a duplicate key error, any advice on what to do? I can't use the original table's primary key as the view is being grouped.

Many thanks,

Jonathan

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

Error:

Msg 1505, Level 16, State 1, Line 1
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.bvw_Grouped_Delphi_optimised' and the index name 'IX_Delphi_Holidex_Code'. The duplicate key value is (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

You must create a unique clustered index. The docs clearly state that there is no way around it.

If your data is not unique on any key this is usually a data-quality issue, a conceptual mistake an outright bug. I therefore advise you to reevaluate this design.

If you insist on doing it like this you must invent/concoct some unique key. Fortunately, all queries with a group-by have such a key: the grouping columns. Add those columns to the view and create the index on them.

This is logically the primary key of your view. Grouping on something results in that "something" being unique in the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM