从两列中选择最大版本号

发布于 2024-10-02 08:02:16 字数 1197 浏览 7 评论 0原文

这与我之前问过的另一个问题有关。如果你快速浏览一下,你可能会对这一点有更好的理解。 版本号浮点数、十进制或双精度

我有两列,数据库表中的外部数据。 [版本] 列和 [修订] 列。这些与版本号有关。 例如版本 1,修订版 2 = v1.2

我需要做的是获取特定外键的最大版本号。

到目前为止,这就是我所拥有的:

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]

基本上抓住最大值并连接回自身。这显然不起作用,因为如果我有版本号 1.11.22.0,我从上述查询返回的最大值是2.2(不存在)。

我需要做的(我认为)是选择最大[版本],然后选择该[版本]的最大[修订],但我不太清楚如何做到这一点。

欢迎任何帮助、建议、问题。

谢谢。

This relates to another question I asked previously. You may have a better understanding of this if you quickly scan it.
Version Numbers float, decimal or double

I have two colums and a foreign in a database table. A [Version] column and a [Revision] column. These are in relation to version numbers. e.g. Version 1, Revision 2 = v1.2

What I need to do is grab the maximum version number for a particular foreign key.

Here's what I have so far:

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]

Basically grabbing the maximum and joining back to itself. This obvisouly doesn't work because if I have version numbers 1.1, 1.2 and 2.0 the maximum value I'm returning from the above query is 2.2 (which doesn't exist).

What I need to do (I think) is select the maximum [Version] and then select the maximum [Revision] for that [Version] but I can't quite figure how to do this.

Any help, suggestions, questions are all welcome.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风柔一江水 2024-10-09 08:02:16

您可以将其更改为

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 会执行相同的操作来检索记录。

You could change it to

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] 

The idea is to multiply the Version with a constant large enough so it never collides with revision (I have taken 100.000 but any value would do).

After that, your JOIN does the same to retrieve the record.

躲猫猫 2024-10-09 08:02:16

下面应该可以提取顶部修订版。

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中。然后,它按修订顺序对这些记录进行降序排序,并提取该组记录的最上面的内容。

The below should work to extract the top revision.

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

Namely, it extracts only the records using the max version into table x. Then it orders these records by revision in descending order, and extracts the topmost of the bunch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文