好的,我找到了一个丑陋的解决方案。我加入了表格以创建当前修订版和 lag_revision。然后我计算了一个新字段(版本)。我的表名是文件。
example:
ID REV LAG_REV Version
Xxx A.1 (null) A.1
Xxx A.3 A.1 A.3
Xxx A A.3 A.4
With numbered_docs as (
Select row_number() over (partition by ID order by creation_date) as rn, d.* from documents d
)
select z.ID,z.REVISION,z.lag_row_rev,z.Version into versions from (
Select d1.ID d1.revision, d2.revision as lag_row_rev,d1.CREATION_DATE
,(select case
when (CHARINDEX('.',d2.REVISION) > 0 and substring(d2.REVISION,1,CHARINDEX('.', d2.REVISION+'.')-1)=substring(d1.REVISION,1,CHARINDEX('.', d1.REVISION+'.')-1))
then (substring(d2.REVISION,1,CHARINDEX('.', d2.REVISION)) + cast((cast(SUBSTRING(d2.REVISION,CHARINDEX('.', d2.REVISION)+1,LEN(d2.revision)) as integer)+1) AS varchar))
when CHARINDEX('.',d1.REVISION) > 0 and CHARINDEX('.',d2.REVISION)=0 then substring(d1.REVISION,1,CHARINDEX('.', d1.REVISION)) + cast((cast(SUBSTRING(d1.REVISION,CHARINDEX('.', d1.REVISION)+1,LEN(d1.revision)) as integer)) AS varchar)
when d2.REVISION IS null and CHARINDEX('.',d1.REVISION) > 0 then substring(d1.REVISION,1,CHARINDEX('.', d1.REVISION)) + cast((cast(SUBSTRING(d1.REVISION,CHARINDEX('.', d1.REVISION)+1,LEN(d1.revision)) as integer)) AS varchar)
when d2.REVISION IS null and CHARINDEX('.',d1.REVISION) = 0 then d1.REVISION+'.1'
when CHARINDEX('.',d1.REVISION) = 0 and CHARINDEX('.',d2.REVISION) = 0 then d1.REVISION+'.1'
when CHARINDEX('.',d2.REVISION) > 0 and CHARINDEX('.',d1.REVISION) > 0 and substring(d2.REVISION,1,CHARINDEX('.', d2.REVISION+'.')-1)<> substring(d1.REVISION,1,CHARINDEX('.', d1.REVISION+'.')-1)
then substring(d1.REVISION,1,CHARINDEX('.', d1.REVISION)) + cast((cast(SUBSTRING(d1.REVISION,CHARINDEX('.', d1.REVISION)+1,LEN(d1.revision)) as integer)) AS varchar)
else NULL
end ) as [Version]
From numbered_docs d1
Left Join numbered_docs d2 on (d1.ID=d2_ID and d1.rn=d2.rn+1)
) z
order by z.ID,z.revision;