Group by and count values

Sharpless512

Im new to Django.

I have different classes (Stock, Portfolio, Trades)

What Im trying to achieve is a summery of all trades per stock.

So Basicly this is how my rows are now

enter image description here

I want to combine or group by stock_id and do a sum of open_price and quantity.

enter image description here

I tried with

Trade.objects.filter(portfolio=1).values('stock__name').aggregate(stock_total=Count('stock',distinct=True))

But that doesnt work.

How do I need to write the query to get that result?

Astik Anand

Try this:

from django.db.models import Count

Trade.objects.filter(portfolio=1).values('stock__name').annotate(stock_total=Count('entry'))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

count all values and group by with count

From Dev

Sum and count values by group

From Dev

Count unique values by group

From Dev

Pandas group by column and count values

From Dev

MySQL GROUP BY with Zero values in the count

From Dev

Count and group on occurrences of keys and their values

From Dev

How to count values in pandas group

From Dev

Group by count for boolean values in collection

From Python

Running count of repeating values by group

From Dev

Count the number of unique values by group

From Dev

Count rows by group based on values

From Dev

R DPLYR Count Values BY Group

From Dev

Count total missing values by group?

From Dev

Count distinct values depending on group

From Dev

MySQL Group by consecutive values and count

From Dev

How to count values in group by with condition?

From Dev

Count values that go in row in group?

From Dev

TSQL Count appearance Group by values in other table

From Dev

SQLite Count of Values in 1 Column and Group By ID

From Mysql

SQL query to get count and group by output values

From Dev

group by and count values from text field

From Dev

Find values with group count greater than 1

From Dev

How to add default values to group by & count in PostgreSQL?

From Dev

Limiting GROUP BY based on COUNT() values in mySQL

From Dev

Prometheus Query - Group by values and return a count

From Dev

How to count number of values per group?

From Dev

Add new column for count of repeated group values

From Dev

How to group by and count values in my DataFrame?

From Mysql

SQL GROUP BY COUNT and Include 0 Values