按列分组返回特定行

5vf7fwbs  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(277)

我被sql查询困住了,我的想法是我有一个数据库,在那里我保存所有文档和每个文档 version(revisionNumber) 等等,我想实现的是,我想访问目前只有那些文件与最新的 revisionNumber .

|  id | title               | documentForm | revisionNumber | effectiveDate |
| --: | ------------------- | -------------| -------------: | :------------ |
|   1 | Event Calendar      | SOP-CL       |            1.0 | 2011-02-02    |
|   2 | Event Calendar      | SOP-CL       |            2.0 | 2012-12-16    |
|   3 | Event Calendar      | SOP-CL       |            3.0 | 2014-02-15    |
|   4 | Event Calendar      | SOP-CL       |            4.0 | 2014-08-01    |
|   5 | Event Calendar      | SOP-CL       |            5.0 | 2016-09-12    |
|   6 | Event Calendar      | SOP-CL       |            6.0 | 2018-09-11    |
|   7 | Software development| SOP-DEV      |            1.0 | 2015-11-25    |
|   8 | Granting and...     | SOP-GRA      |            1.0 | 2014-08-04    |
|   9 | Granting and...     | SOP-GRA      |            2.0 | 2015-12-07    |
|  10 | Granting and...     | SOP-GRA      |            3.0 | 2018-03-26    |

在这里您可以看到查询后我需要得到的结果:

|  id | title               | documentForm | revisionNumber | effectiveDate |
| --: | ------------------- | ------------ | -------------: | :------------ |
|   6 | Event Calendar      | SOP-CL       |            6.0 | 2018-09-11    |
|   7 | Software development| SOP-CL       |            1.0 | 2015-11-25    |
|   3 | Granting and...     | SOP-GRA      |            3.0 | 2018-03-26    |

我在google上搜索发现,可以通过分组(比如documentform)和返回来完成 MAX(revisionNumber) 但我没有找到正确的一行 id 以及 effectiveDate . 我想我只是用得不对。

rdrgkggo

rdrgkggo1#

您可以尝试在where子句中使用子查询。
架构(mysql v5.6)

CREATE TABLE t (
  title varchar(50),
  documentForm varchar(50), 
  effectiveDate date,
  revisionNumber int

);

insert into t values ('vent Calendar','SOP-CL','2011-02-02',1.0);
insert into t values ('vent Calendar','SOP-CL','2012-12-16',2.0);
insert into t values ('vent Calendar','SOP-CL','2014-02-15',3.0);
insert into t values ('vent Calendar','SOP-CL','2014-08-01',4.0);
insert into t values ('vent Calendar','SOP-CL','2016-09-12',5.0);
insert into t values ('vent Calendar','SOP-CL','2018-09-11',6.0);
insert into t values ('oftware development ','SOP-DEV','2015-11-25',1.0);  
insert into t values ('ranting and..','SOP-GRA','2014-08-04',1.0);
insert into t values ('ranting and..','SOP-GRA','2015-12-07',2.0);
insert into t values ('ranting and..','SOP-GRA','2018-03-26',3.0);

查询#1

SELECT * 
FROM t t1
WHERE revisionNumber  = (
   select max(tt.revisionNumber)
   from t tt
   WHERE t1.documentForm = tt.documentForm
);

| title                | documentForm | effectiveDate | revisionNumber |
| -------------------- | ------------ | ------------- | -------------- |
| vent Calendar        | SOP-CL       | 2018-09-11    | 6              |
| oftware development  | SOP-DEV      | 2015-11-25    | 1              |
| ranting and..        | SOP-GRA      | 2018-03-26    | 3              |

db fiddle视图

ujv3wf0j

ujv3wf0j2#

使用相关子查询

select * from table1 t1
where revisionNumber in ( select max(revisionNumber) 
   from
   table1 t2 where t1.title=t2.title and t1.documentForm=t2.documentForm
   group by t2.title,t2.documentForm
   )
v8wbuo2f

v8wbuo2f3#

有一个子查询 GROUP BY 返回每个文档表单及其最高版本修订号。 JOIN 结果是:

select t1.*
from tablename t1
join (select documentForm, version(revisionNumber) as maxrevisionNumber
      from tablename
      group by documentForm) t2
    on  t1.documentForm = t2.documentForm
    and t1.revisionNumber = t2.maxrevisionNumber

相关问题