首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过分组列返回特定行

通过分组列返回特定行
EN

Stack Overflow用户
提问于 2018-09-12 06:55:44
回答 3查看 32关注 0票数 0

我被SQL查询困住了,我的想法是我有一个数据库,在这里我保存所有文档和每个文档version(revisionNumber)等等。我想要实现的是,我现在只想用最新的revisionNumber访问那些文档。

代码语言:javascript
复制
|  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    |

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

代码语言:javascript
复制
|  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    |

我一直在谷歌上搜索,发现可以通过分组(例如,documentForm和返回MAX(revisionNumber) )来完成,但是我没有得到正确的行ideffectiveDate。我想我只是没有正确地使用它们。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-09-12 06:58:46

使用相关子查询

代码语言:javascript
复制
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
   )
票数 2
EN

Stack Overflow用户

发布于 2018-09-12 07:00:27

您可以尝试在where子句中使用子查询。

模式(MySQL v5.6)

代码语言:javascript
复制
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

代码语言:javascript
复制
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的看法

票数 2
EN

Stack Overflow用户

发布于 2018-09-12 06:59:05

有一个执行GROUP BY的子查询来返回每个documentForm的最高版本revisionNumber。JOIN的结果如下:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52289138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档