繁体   English   中英

根据多列选择最大值列

[英]select max valued column row based on multiple columns

我有以下表格documents

+--------+-------------+------------+----------+---------+
| doc_id | module_name | mapping_id | doc_type | version |
+--------+-------------+------------+----------+---------+
|      5 | asdf        | asd        | POI      |       1 |
|      6 | asdf        | asd        | POI      |       2 |
|      7 | asdf        | asd        | CAF      |       1 |
|      8 | asdf        | abc        | POI      |       1 |
|      9 | asdf        | abc        | ISR      |       1 |
|     10 | asdf        | abc        | ISR      |       2 |
|     11 | asdf        | xyz        | POA      |       1 |
|     12 | asdf        | xyz        | CAF      |       1 |
|     13 | asdf        | xyz        | CAF      |       2 |
|     14 | asdf        | xyz        | CAF      |       3 |
|     15 | pqrs        | asd        | POI      |       1 |
|     16 | pqrs        | asd        | POI      |       2 |
|     17 | pqrs        | asd        | CAF      |       1 |
|     18 | pqrs        | abc        | POI      |       1 |
|     19 | pqrs        | abc        | ISR      |       1 |
|     20 | pqrs        | abc        | ISR      |       2 |
|     21 | pqrs        | xyz        | POA      |       1 |
|     22 | pqrs        | xyz        | CAF      |       1 |
|     23 | pqrs        | xyz        | CAF      |       2 |
|     24 | pqrs        | xyz        | CAF      |       3 |
+--------+-------------+------------+----------+---------+

我想得到的结果是:

+--------+-------------+------------+----------+---------+
| doc_id | module_name | mapping_id | doc_type | version |
+--------+-------------+------------+----------+---------+
|      6 | asdf        | asd        | POI      |       2 |
|      7 | asdf        | asd        | CAF      |       1 |
|      8 | asdf        | abc        | POI      |       1 |
|     10 | asdf        | abc        | ISR      |       2 |
|     11 | asdf        | xyz        | POA      |       1 |
|     14 | asdf        | xyz        | CAF      |       3 |
|     16 | pqrs        | asd        | POI      |       2 |
|     17 | pqrs        | asd        | CAF      |       1 |
|     18 | pqrs        | abc        | POI      |       1 |
|     20 | pqrs        | abc        | ISR      |       2 |
|     21 | pqrs        | xyz        | POA      |       1 |
|     24 | pqrs        | xyz        | CAF      |       3 |
+--------+-------------+------------+----------+---------+

其中应以module_namemapping_iddoc_type组合返回max版本号行。

我无法弄清楚查询。 需要帮忙。

在大多数数据库中,您将使用row_number() 在MySQL中,此功能不可用。 一种方法是joingroup by

select t.*
from t join
     (select module_name, mapping_id, doc_type, max(version) as version
      from t
      group by module_name, mapping_id, doc_type
     ) tt
     using (module_name, mapping_id, doc_type, version);

如果我们按字面意思考虑OP的话,或者可能只是

select t.*
from t join
     (select doc_type, max(version) as version
      from t
      group by doc_type
     ) tt
     using (doc_type, version);

另一种选择

select *
from documents d
where not exists
  (
    select 1 from documents dv
    where dv.doc_type = d.doc_type
      and dv.version > d.version
  )

从字面上看: “对于相同的值,没有更大的版本号”

暂无
暂无

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

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