簡體   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