django - Django ORM - 使用 PostgreSQL 在同一个表上的 GROUP BY 子查询的内部联接
问题描述
我有一个看起来像这样的表(我省略了一些列):
ID | Key_ID | 项目_ID | 其他栏目 |
---|---|---|---|
1 | 1 | 123456 | 一个 |
2 | 1 | 123456 | b |
3 | 2 | 123456 | C |
4 | 2 | 123456 | d |
5 | 3 | 654321 | e |
6 | 3 | 654321 | F |
7 | 4 | 654321 | G |
8 | 4 | 654321 | H |
使用 Django ORM,我需要得到这个查询的等价物:
SELECT * FROM table AS t
INNER JOIN (
SELECT MAX(ID) AS max_ID FROM table
GROUP BY Key_ID
WHERE Proeject_ID = 123456
) AS sub_query
ON t.ID = sub_query.max_ID
我已经尝试了一些aggregate
和annotate
组合,但我似乎无法GROUP BY
在子查询中实现。如果我能做到这一点,我可以尝试.filter(id__in=<subuery_result>
如此有效地使用 aSELECT ... WHERE ID IN <subquery_result
虽然这INNER JOIN
将是理想的,因为子查询结果可能非常大。
更新:
我使用的数据库是 PostgreSQL,并且接受的答案只适用于此。
这是实际模型:
class SystemKey(models.Model):
# The ID (primary key) is handled by Django.
key_id = models.PositiveIntegerField(
help_text="Unique key ID from System."
)
project = models.ForeignKey(
"core.SystemProject",
on_delete=models.PROTECT,
help_text="System project that this key belongs to.",
)
# There are a whole bunch of other properties here
record_created = models.DateTimeField(
auto_now_add=True,
help_text="Date & time when this record was added in the database.",
)
record_updated = models.DateTimeField(
auto_now=True,
help_text="Date & time when this record was updated in the database.",
)
解决方案
您可以进行后续调用order_by
并distinct
在传递字段时distinct
有效地实现您想要的,唯一需要注意的是这仅适用于 PostgreSQL:
SystemKey.objects.filter(project_id=123456).order_by('key_id', '-id').distinct('key_id')
基本上在这里我们选择具有不同的条目key_id
,因为我们按id
降序排序,所以我们只得到每个 id 最大的条目key_id
。
推荐阅读
- php - “字段列表”中的未知列“activation_code”
- json - jq - 将值插入子键
- python - 对 pandas 数据框中的列进行分类
- php - 在PHP中使用这种格式的邮件无回复是正确的
- c - 声明有大小和没有大小的指针之间的区别
- intellij-idea - WebStorm 中的 NextJS 支持?
- jenkins - 詹金斯 - 如果比较 HOUR 的语句不起作用
- javascript - 将基于 materila-kit 的项目连接到数据库的问题
- javascript - 通过 Selenium 和手动启动的 IE 浏览器交互的差异
- java - 连接 2 个整数