这是可以为您完成工作的东西。想法是使用临时表,它也包含不带 _v 后缀的文件名。
我可能已经使这比需要的更复杂,但您将能够明白这一点
DROP TABLE #TmpResults
CREATE TABLE #TmpResults
(
Original nvarchar(100),
WO_Version nvarchar(100),
Last_Update datetime
)
INSERT INTO #TmpResults
(Original, WO_Version, Last_Update)
VALUES
('file1.xml', 'file1.xml', '01/01/2013'),
('file2.xml', 'file2.xml', '02/01/2013'),
('file2_v2.xml', 'file2.xml', '03/01/2013'),
('file3.xml', 'file3.xml', '01/01/2013'),
('file3_v2.xml', 'file3.xml', '01/02/2013'),
('file3_v3.xml', 'file3.xml', '01/03/2013'),
('file4.xml', 'file4.xml', '05/01/2013'),
('file5.xml', 'file5.xml', '06/01/2013'),
('file5_v2.xml', 'file5.xml', '06/02/2013'),
('file5_v3.xml', 'file5.xml', '06/03/2013'),
('file5_v4.xml', 'file5.xml', '06/04/2013')
SELECT
P.WO_Version,
(SELECT MAX(Last_Update) FROM #TmpResults T WHERE T.WO_Version =
P.WO_Version) as Last_Update,
(SELECT TOP 1 Original
FROM #TmpResults T
WHERE T.Last_Update =
( SELECT MAX(Last_Update)
FROM #TmpResults Tm
WHERE Tm.WO_Version = P.WO_Version) ) as Last_FileVersion
FROM
(
SELECT DISTINCT WO_Version
FROM #TmpResults
GROUP BY WO_Version
) P
这是您可以使用 SELECT INTO 填充临时表的 SELECT 查询
SELECT
Original_File_Name,
REPLACE(@Original_File_Name,
SUBSTRING(@Original_File_Name, LEN(@Original_File_Name) - CHARINDEX('v_',REVERSE(@Original_File_Name), 1), LEN(@Original_File_Name) - CHARINDEX('v_',REVERSE(@Original_File_Name), 1)),
SUBSTRING(@Original_File_Name, LEN(@Original_File_Name) - CHARINDEX('.',REVERSE(@Original_File_Name), 1) +1 , LEN(@Original_File_Name) - CHARINDEX('.',REVERSE(@Original_File_Name), 1))) as WO_Version,
Last_Update
FROM OriginalDataTable