简体   繁体   English

选择具有另一列最大值的不同行

[英]Select distinct rows with max value of another column

I am querying a document management database where documents can have many versions.我正在查询文档管理数据库,其中文档可以有多个版本。 So my query below returns the same document multiple times because of the versioning.因此,由于版本控制,我下面的查询多次返回相同的文档。

 SELECT distinct d.DOCNUM, d.DOCNAME,P.FolderName
    FROM DOCUMENT as d with (nolock)
    inner join Project_T as pt on d.docnum=pt.item_id
    inner join Projects p on pt.prj_id=p.prj_id
   where d.type = 'Personal' and d.owner like '%67360'

Results:结果:

DOCNUM     | DOCNAME     | FOLDERNAME |
-----------+----------+---------------+
123         Article        Jonathan
123         Article part1  Jonathan
256         Meeting Notes  Jonathan
5697        Memo           Jonathan

Ideally docnum 123 should only return once.理想情况下 docnum 123 应该只返回一次。

I have joined the table that holds the versions for each document and am selecting the latest version (MAX(h.version) so my query should return unique document numbers per row.我加入了保存每个文档版本的表,并选择了最新版本(MAX(h.version)因此我的查询应该每行返回唯一的文档编号。

    SELECT distinct d.DOCNUM, h.version, d.DOCNAME,P.FolderName
    FROM DOCUMENT as d with (nolock)
    inner join DOCHISTORY as h on h.DOCNUM = d.docnum
    inner join Project_T as pt on d.docnum=pt.item_id
    inner join Projects p on pt.prj_id=p.prj_id
   where d.type = 'Personal' and d.owner like '%67360'
   and d.SECURITY = 'P'
   AND h.VERSION = (SELECT MAX(x.version) FROM DOCHISTORY as X 
                    where x.docnum = d.DOCNUM) 

but this time I get the latest versions however, where docname is slightly different, it returns the same docnum multiple times.但是这次我得到了最新版本,其中 docname 略有不同,它多次返回相同的 docnum。 see docnum 123 below.请参阅下面的文档 123。

DOCNUM     | Version      | DOCNAME     | FOLDERNAME |
-----------+--------------+-------------+------------+
123          9              Article        Jonathan
123          9              Article part1  Jonathan
256          1              Meeting Notes  Jonathan
5697         21             Memo           Jonathan

I need to display the docname column in my report.我需要在我的报告中显示 docname 列。 Is there a way aorund this please?请问这个有办法吗?

Yes, you can select the top 1 docname.是的,您可以选择前 1 个文档名。 Replace your field selection list with this:将您的字段选择列表替换为:

SELECT distinct d.DOCNUM
 ,h.version 
 ,DOCNAME = (SELECT TOP 1 d2.DOCNAME FROM DOCUMENT d2, DOCHISTORY h2 
 WHERE d2.DOCNUM = d.DOCNUM and h2.DOCNUM = d.docnum)
 ,P.FolderName

....(rest of code) ....(其余代码)

In the above output, doc 123 version 9 should appear as Article.在上面的输出中,doc 123 version 9 应该显示为 Article。

What you are trying to do is much easier with window functions.使用窗口函数,您要做的事情要容易得多。 Here is one way:这是一种方法:

select DOCNUM, version, DOCNAME, FolderName
from (SELECT d.DOCNUM, h.version, d.DOCNAME, P.FolderName,
             max(h.version) over (partition by d.docnum) as maxversion
      FROM DOCUMENT  d with (nolock)
           inner join DOCHISTORY as h on h.DOCNUM = d.docnum
           inner join Project_T as pt on d.docnum=pt.item_id
           inner join Projects p on pt.prj_id=p.prj_id
     where d.type = 'Personal' and d.owner like '%67360' and d.SECURITY = 'P'
    ) d
where version = maxversion;

The expression max(h.version) over (partition by d.docnum) is a construct called a window function (or analytic function in some databases).表达式max(h.version) over (partition by d.docnum)是一种称为窗口函数(或某些数据库中的分析函数max(h.version) over (partition by d.docnum)的构造。 What is does is return the maximum of h.version for each value of d.docnum (based on the partition by clause).所做的是为h.version的每个值返回h.versiond.docnum (基于partition by子句)。 This is ANSI standard SQL and most databases support this functionality.这是 ANSI 标准 SQL,大多数数据库都支持此功能。

Your version is probably not working because the subquery that calculates the maximum value is not using the same filtering criteria as the outer query.您的版本可能无法正常工作,因为计算最大值的子查询未使用与外部查询相同的过滤条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM