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

39 Views Asked by At

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

process_id qty
10 27
12 12

I tried in this way

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

1

There are 1 best solutions below

4
Simon Notley On

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