我有使用主要版本和次要版本进行版本控制的文件 现在我想要做的就是获取所有文件的最高主要版本(我已经完成了)。然后检查次要版本以查看它是否包含0.如果它确实丢弃了整套版本。所以举个例子。我的第一个查询返回:
FileA Ver 1.0, 1.1, 1.2, 1.3
FileB Ver 2.1, 2.2, 2.3, 2.4
FileC Ver 5.1, 5.2, 5.3.
所以在所有11行/记录中。现在我的第二个查询应该采用该结果并抛弃所有FileA版本,因为其中一个版本的0为次要版本。所以第二个查询应该返回:
FileB Ver 2.1, 2.2, 2.3, 2.4
FileC Ver 5.1, 5.2, 5.3.
共7行/记录。 有人可以帮我解决这个问题吗?如果有帮助,我正在使用SQL Server 2008。
latestFileMajorVersion(fileId, majRev)
as
(
--Gets files with highest major version
select distinct v_fileid, max(v_majrev)
from files
group by v_fileid
),
latestFileVersions(fileId, majRev, minRev)
as
(
--Gets files with highest major version and all minor versions
select fileId, majRev, minrev
from files
inner join latestFileMajorVersion on files.v_fileid = latestFileMajorVersion.fileId
where v_majrev = latestFileMajorVersion.majRev
),
latestFileVersion(fileId, majRev, minRev)
as
(
select fileId, majRev, minrev
from latestFileVersions
where --I'm stuck here.
)
select v_fileid, v_majrev, v_minrev, v_status
from files
inner join latestFileVersion on v_fileid = latestFileVersion.fileId
where latestFileVersion.majRev = v_majrev and latestFileVersion.minRev = v_minrev and v_status = 'UnAssigned'
表格:
CREATE TABLE [dbo].[Files](
[v_fileid] [uniqueidentifier] NOT NULL,
[v_libid] [uniqueidentifier] NOT NULL,
[v_userid] [uniqueidentifier] NOT NULL,
[v_majrev] [int] NOT NULL,
[v_minrev] [int] NOT NULL,
[v_status] [nvarchar](16) NOT NULL,
CONSTRAINT [PK_Files] PRIMARY KEY CLUSTERED
(
[v_fileid] ASC,
[v_majrev] ASC,
[v_minrev] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_Files] UNIQUE NONCLUSTERED
(
[v_majrev] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_Files] UNIQUE NONCLUSTERED
(
[v_minrev] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
答案 0 :(得分:1)
您可以使用此构思重写查询。它适用于您提供的样本数据。
declare @t table (fileid varchar(10), [version] varchar(5))
insert @t values('FileA','1.0')
insert @t values('FileA','1.1')
insert @t values('FileA','1.2')
insert @t values('FileA','1.3')
insert @t values('FileB','2.1')
insert @t values('FileB','2.2')
insert @t values('FileB','2.3')
insert @t values('FileB','2.4')
insert @t values('FileC','5.1')
insert @t values('FileC','5.2')
insert @t values('FileC','5.3')
SELECT * FROM @t t
WHERE NOT EXISTS
(SELECT 1 FROM @t WHERE PARSENAME([version], 1) = '0' AND t.fileid = fileid)
答案 1 :(得分:0)
这是一个可以使用的小示例表的工作解决方案。完全按原样执行它,周围没有其他sql。然后,您可以根据需要对其进行修改。
declare @files table (fileId int identity(1,1), majRev int, minRev int)
insert into @files values (1,0)
insert into @files values (1,1)
insert into @files values (2,0)
insert into @files values (2,2)
insert into @files values (3,1)
insert into @files values (3,2)
insert into @files values (4,1)
select fileId, majRev, minRev
from @files f
where not exists
(select *
from @files
where minRev = 0
and majRev = f.majRev
and fileId = f.fileId)
答案 2 :(得分:0)
当我尝试编写某些内容时,有时候我会过于专注于制作一个“优雅”的解决方案,而不是仅仅强制解决方案并将其应用于特定问题。
所以我想知道是否1)我已经强行推行这个解决方案2)是否有更“优雅”或聪明的方式去写这个。欢迎所有评论,批评和/或建议。
;with solution(fileId, majrev, minrev, status, checkid, maxrev)
as
(
select *
from files
inner join
(
select v_fileid, MAX(v_majrev) as max_major
from files
group by v_fileid
) as max_versions on max_versions.v_fileid = files.v_fileid and
max_major = files.v_majrev
except
select *
from files
inner join
(
select v_fileid, MAX(v_majrev) as max_major
from files
where v_minrev = 0
group by v_fileid
) as unwanted_versions on unwanted_versions.v_fileid = files.v_fileid and
max_major = files.v_majrev
)
select *
from solution