简体   繁体   English

如何在 django 或 postgres 中应用 distinct 和 group by?

[英]how to apply distinct and group by in django or in postgres?

table production餐桌制作

code代码 part部分 qty数量 process_id进程号
1 1个 21 21 10 10 10 10
1 1个 22 22 12 12 10 10
2 2个 22 22 15 15 10 10
1 1个 21 21 10 10 12 12
1 1个 22 22 12 12 12 12

I have to extract data like based on process, every process has multiple part but I can't take every part's data, so that have to distinct on code for getting process wise summation of qty.我必须像基于流程一样提取数据,每个流程都有多个部分,但我不能获取每个部分的数据,因此必须区分代码以获得流程明智的数量总和。 how to get data like this in postgresql or in django如何在 postgresql 或 django 中获取这样的数据

process_id进程号 qty数量
10 10 27 27
12 12 12 12

I tried in this way我这样试过

Production.objects.values('process').distinct('code').annotate(total_qty=Sum('quantity'))

The following query gets your desired result, but from your snippet I'm not sure this is the logic you had in mind.以下查询获得了您想要的结果,但从您的代码片段中我不确定这是您想到的逻辑。 If you add more detail I can refine the answer.如果您添加更多细节,我可以完善答案。

SELECT process_id, SUM(qty) qty
FROM production
WHERE part=22
GROUP BY process_id

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

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