postgresql - SQL MAX 连接查询返回多行

标签 postgresql join max

我写了一个小的选择查询,它是我用来从 PostgreSQL DB 的几个表中检索具有附加属性的项目的较大查询的一部分。

SELECT 
  isni.inventory_id_inventory,
  max(isni.issue_note_id_issue_notes) AS latest_issue_note,
  isn.direction
FROM issue_note_items isni
JOIN issue_notes isn ON isni.issue_note_id_issue_notes = isn.id
GROUP BY isni.inventory_id_inventory, isn.direction

我只需要为每个具有 inventory_id_inventory 键且具有最高 issue_note_id_issue_notes 值的表获取一条记录。或者换句话说,我想找到一个,最后一个问题说明,其中包含每个不同库存 ID 的方向。

我得到的是一两行 - 如果有两个不同的方向(此列中可能有两个小的 int 值)。看起来很简单,应该使用左连接修复,但它不能按预期工作可能是因为 GROUP BY 子句。我应该怎么做才能使其按预期工作?

表格和示例数据:
issue_notes

ID  direction
1   1
2   2
3   2
4   1
5   2
6   1

issue_note_items

ID  inventory_id_inventory  issue_note_id_issue_notes
1   12                      1
2   123                     1
3   12                      2
4   12                      4
5   35                      4
6   123                     5
7   35                      6

预期输出:

ID  inventory_id_inventory  issue_note_id_issue_notes  direction
4   12                      4                          1
6   123                     5                          2
7   35                      6                          1

非常感谢您的帮助

最佳答案

关系数据库中的行没有顺序。除非指定顺序,否则没有“最后”条目。

在这种情况下,顺序似乎是按每个 inventory_id_inventoryinventory_id_inventory 升序排列的。给定该顺序,可以使用 ROW_NUMBER() 排名函数计算每个连接结果的排名,例如:

SELECT 
  isni.id,
  isni.inventory_id_inventory,
  isni.issue_note_id_issue_notes,
  isn.direction,
  ROW_NUMBER() OVER(partition by inventory_id_inventory 
                    order by issue_note_id_issue_notes desc) as rn
FROM issue_note_items isni
JOIN issue_notes isn ON isni.issue_note_id_issue_notes = isn.issue_note_id

partition by inventory_id_inventory 意味着我们希望每个分区按 inventory_id_inventory 单独排名。 order by issue_note_id_issue_notes desc 将为最大 ID 返回 1,从而更容易过滤“最新”条目。

不可能在 where 子句中引用 rn 函数。这可以通过使用 CTE 来解决:

with notes as (
    SELECT 
      isni.id,
      isni.inventory_id_inventory,
      isni.issue_note_id_issue_notes,
      isn.direction,
      ROW_NUMBER() OVER( partition by inventory_id_inventory 
                         order by issue_note_id_issue_notes desc) as rn
    FROM issue_note_items isni
    JOIN issue_notes isn ON isni.issue_note_id_issue_notes = isn.issue_note_id      
)
select * 
from notes
where rn=1
order by issue_note_id_issue_notes

使用以下数据:

create table issue_notes (issue_note_id int,direction int);
create table issue_note_items (id int, inventory_id_inventory  int,issue_note_id_issue_notes int);

insert into issue_notes
values
(1,1),
(2,2),
(3,2),
(4,1),
(5,2),
(6,1);

insert into issue_note_items 
values
(1,12 ,1),
(2,123,1),
(3,12 ,2),
(4,12 ,4),
(5,35 ,4),
(6,123,5),
(7,35 ,6);

结果将是:

|id |inventory_id_inventory |issue_note_id_issue_notes  |direction  |rn
|4  |12                     |4                          |1          |1
|6  |123                    |5                          |2          |1
|7  |35                     |6                          |1          |1

SQL fiddle here

关于postgresql - SQL MAX 连接查询返回多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51376524/

相关文章:

SQL:如何在考虑时间限制的情况下对各组进行平均

mysql - 将多个表合并为一个表

wpf - 在不使用 anchor 的情况下通过一条线连接两个 WPF Canvas 元素?

Javascript-如何编写一个函数来查找任何一组数字参数中的最大数字

javascript - 使用javascript获取数组的第一和第二高值

c# - 为什么 PostgreSQL 将查询中的值视为列名?

sql - 必须出现在sql中的group by子句中

sql - 如何在SQL中使用MAX函数?

javascript - 尝试使用连接器表连接 Sequelize 中的两个数据库表

mysql - SQL - 如何在 JOIN 条件中执行 IF/ELSE