我有以下查询给我一个ROW_NUMBER,作为正在进行的案例中的“阶段”。此查询在INSERT INTO下运行以填充表。 在那一刻,我们不得不每个月完全重新构建表,以使用连续的行号捕获阶段中的每个更改(当进程继续时,每个阶段的一行)。在某些时候,表格会变得太大而无法做到这一点,我希望能够追加,但问题是,我想不出一种方法可以让行号从中断处继续。我在测试表中尝试了它,当我运行查询两次,分别在2015年11月进行两半时,每个案例中从11月上半月到下半年的任何阶段的行数都从1开始,如果是在第二轮中,而不是继续。
ModifiedRecordID列标识了案例,您可以看到PARTITION BY被分组的内容。
有没有办法做我正在考虑的事情?
SELECT agc.ObjectTypeCode
, ev.Name AS EntityName
, agc.AttributeId
, ea.AttributeName
, ind.createdbyname AS CaseCreatedBy
, agc.CallingUserName
, agc.CallingUserId
, LEFT(CAST(agc.OldFieldValue AS NVARCHAR(MAX)), ISNULL(NULLIF(CHARINDEX(',', CAST(agc.OldFieldValue AS NVARCHAR(MAX))) -1, -1), '')) AS ChangedField
, UPPER(RIGHT(CAST(agc.OldFieldValue AS NVARCHAR(MAX)), LEN(CAST(agc.OldFieldValue AS NVARCHAR(MAX))) - CHARINDEX(',', CAST(agc.OldFieldValue AS NVARCHAR(MAX))))) AS PreviousGuidValue
, wkt.ptl_name AS WorkType
, ModifiedRecordId
, ind.ticketnumber AS CaseRef
, agc.ActionId
, ind.createdon AS MatterCreated
, agc.LogDateTime AS AuditedDate
, agc.AuditId
, ROW_NUMBER() OVER(PARTITION BY ModifiedRecordId ORDER BY agc.LogDateTime ASC) Stage
FROM AuditGuidChange agc LEFT JOIN EntityView ev
ON agc.ObjectTypeCode = ev.ObjectTypeCode
LEFT JOIN EntityAttribute ea
ON agc.AttributeId = ea.ColumnNumber
AND agc.ObjectTypeCode = ea.MappedObjectCode
LEFT JOIN Peppermint_Data.dbo.incident ind
ON agc.modifiedrecordid = ind.incidentid
LEFT JOIN Filteredptl_worktype wkt
ON agc.PreviousGuidValue = wkt.ptl_worktypeid
WHERE LEFT(CAST(agc.OldFieldValue AS NVARCHAR(MAX)), ISNULL(NULLIF(CHARINDEX(',', CAST(agc.OldFieldValue AS NVARCHAR(MAX))) -1, -1), '')) = 'ptl_worktype'
AND CAST(agc.LogDateTime AS DATE) BETWEEN @sDate AND @eDate;
答案 0 :(得分:1)
declare @offset int = 12;
select id, ROW_NUMBER() over (order by id) + @offset as rn
from table;