I have files that are versioned, using a major and minor version.
Now what I want to do is grab the highest major versions of all the files (which I’ve done already). Then check minor versions to see if it contains a 0. If it does throw out that whole set of versions. So for instance. My first query returns:
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.
So in all 11 rows/records. Now my second query should take that result and throw out all of the FileA versions because one of the versions has a 0 as a minor version. So the second query should return:
FileB Ver 2.1, 2.2, 2.3, 2.4
FileC Ver 5.1, 5.2, 5.3.
7 rows/records in all.
Could anyone help me with this query? I’m using SQL server 2008 if that helps.
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'
The table:
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]
Here’s a working solution with a little example table you can play with. Execute it exactly as it is with no other sql around it. You can then modify it to suit your need.