[英]how to apply distinct and group by in django or in postgres?
table production餐桌制作
code![]() |
part![]() |
qty![]() |
process_id![]() |
---|---|---|---|
1 ![]() |
21 ![]() |
10 ![]() |
10 ![]() |
1 ![]() |
22 ![]() |
12 ![]() |
10 ![]() |
2 ![]() |
22 ![]() |
15 ![]() |
10 ![]() |
1 ![]() |
21 ![]() |
10 ![]() |
12 ![]() |
1 ![]() |
22 ![]() |
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 ![]() |
27 ![]() |
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.