Choosing the maximum version number from two columns

This refers to another question that I asked earlier. You might be better off understanding this if you quickly look through it. Version numbers are floating, decimal, or double

I have two columns and a stranger in the database table. A [Version] and the [Revision] column . They relate to version numbers. e.g. Version 1, version 2 = v1.2

What I need to do is to capture the maximum version number for a specific foreign key.

Here is what I still have:

SELECT f.[pkFileID]
   ,x.[fkDocumentHeaderID]
   ,f.[fkDocumentID]
   ,x.[Version]
   ,x.[Revision]
   ,f.[FileURL]
   ,f.[UploadedBy]
   ,f.[UploadedDate]
FROM 
(
     SELECT 
     docs.[fkDocumentHeaderID]
     ,MAX([Version]) AS Version
     ,MAX([Revision]) AS Revision
 FROM 
     [ClinicalGuidanceV2].[dbo].[tbl_DocumentFiles]
 INNER JOIN 
     dbo.tbl_Documents docs ON [fkDocumentID] = [pkDocumentID]
 GROUP BY
     docs.[fkDocumentHeaderID]
)
AS x
INNER JOIN
 dbo.tbl_DocumentFiles f ON 
 f.[fkDocumentHeaderID] = x.[fkDocumentHeaderID] AND 
 f.[Version] = x.[Version] AND
 f.[Revision] = x.[Revision]

. obvisouly , , 1.1, 1.2 2.0, , , 2.2 ( ).

( ), [], [] [], , .

, , .

.

+3
2

SELECT  f.[pkFileID]
        ,x.[fkDocumentHeaderID]
        ,f.[fkDocumentID]
        ,x.[Version]
        ,x.[Revision]
        ,f.[FileURL]
        ,f.[UploadedBy]
        ,f.[UploadedDate]
FROM    (
          SELECT  docs.[fkDocumentHeaderID]
                  ,MAX([Version] * 100000 + [Revision]) AS [VersionRevision] 
          FROM    [ClinicalGuidanceV2].[dbo].[tbl_DocumentFiles]
                  INNER JOIN dbo.tbl_Documents docs 
                    ON [fkDocumentID] = [pkDocumentID]
          GROUP BY
                  docs.[fkDocumentHeaderID]
        )AS x
        INNER JOIN dbo.tbl_DocumentFiles f 
          ON f.[fkDocumentHeaderID] = x.[fkDocumentHeaderID] 
             AND f.[Version] * 100000 + f.[Revision] = x.[VersionRevision] 

, , , ( 100 000, ).

JOIN , .

+2

, .

SELECT TOP 1 f.[pkFileID]
   ,x.[fkDocumentHeaderID]
   ,f.[fkDocumentID]
   ,x.[Version]
   ,x.[Revision]
   ,f.[FileURL]
   ,f.[UploadedBy]
   ,f.[UploadedDate]
FROM 
(
     SELECT 
     docs.[fkDocumentHeaderID]
     ,MAX([Version]) AS Version
     -- Comment this out ,MAX([Revision]) AS Revision
 FROM 
     [ClinicalGuidanceV2].[dbo].[tbl_DocumentFiles]
 INNER JOIN 
     dbo.tbl_Documents docs ON [fkDocumentID] = [pkDocumentID]
 GROUP BY
     docs.[fkDocumentHeaderID]
)
AS x
INNER JOIN
 dbo.tbl_DocumentFiles f ON 
 f.[fkDocumentHeaderID] = x.[fkDocumentHeaderID] AND 
 f.[Version] = x.[Version] 
ORDER BY x.Revision DESC

, , x. .

0

Source: https://habr.com/ru/post/1775190/


All Articles