I have the following tables:create table item1(tranid int not null, id int not null, partitionid int not null, date datetime, scangap int)alter table item1 add constraint pk_item1 primary key (tranID, id, partitionid)alter table item1 add constraint ck_item1 check (PartitionID = 1)create table item2(tranid int not null, id int not null, partitionid int not null, date datetime, scangap int)alter table item2 add constraint pk_item2 primary key (tranID, id, partitionid)alter table item2 add constraint ck_item2 check (PartitionID = 2)
And the following view:create view vwItemasselect * from item1union all select * from item2
To populate the tables, run these statements:declare @ItemID int, @tranID intset @tranID = 1set @ItemID = 1while @ItemID < 4begin waitfor delay '00:00:02' insert into vwItem select @tranID, @ItemID, @tranID, current_timestamp, 0 set @ItemID = @ItemID+1endset @tranID = 2set @ItemID = 1while @ItemID < 4begin waitfor delay '00:00:04' insert into vwItem select @tranID, @ItemID, @tranID, current_timestamp, 0 set @ItemID = @ItemID+1end
Aim: To get the difference in time between when one record was inserted to the next(datediff(ss, a.date,b.date):declare @partition intset @partition = 1select i.tranID, i.ID, datediff(ss,a.date,i.date)from vwitem iinner join (select tranID, id, date, partitionID from vwitem where partitionID= @partition) as a on a.tranid = i.tranid and i.id = a.id + 1and a.PartitionID = i.partitionID
Results:tranID ID Time Diff----------- ----------- -----------1 2 21 3 41 4 41 5 4
I created the following query:declare @partition intset @partition = 1update vwitemset scangap = datediff(ss,a.date,i.date)from vwitem iinner join (select tranID, id, date, partitionID from vwitem where partitionID= @partition) as a on a.tranid = i.tranid and i.id = a.id + 1and a.PartitionID = i.partitionID
But get the following error:Msg 4439, Level 16, State 6, Line 4Partitioned view 'CRDM_Test Prototype.dbo.vwItem' is not updatable because the source query contains references to partition table '[CRDM_Test Prototype].[dbo].[item1]'.I understand that this rule applies to partitioned views (from BOL):'INSERT, UPDATE, and DELETE actions against a partitioned view are not allowed if there is a self-join with the same view or with any of the member tables in the statement.'But I thought a self-join is more something like so:select * from a inner join a on a.employeeid = a.manageridWhereas I am doing a subselect? Also, if this is not possible, then how can I achieve my goal without doing a self join? Also, the size of each partitioned table is about 7/8million rowsThanks in advanceHearty head pats