Get First element by the recent date of each group

user1584253

I have following model in django

Business ID
Business Name
Business Revenue
Date

Here is the sample data:

Business ID | Business Name | Business Revenue | Date
1             B1              1000               2012-10-13
2             B2              20000              2013-10-16
1             B1              2000               2013-04-13
2             B2              24000              2014-09-02

I want to fetch all businesses having latest date.

Here is the output I want:

Business ID | Business Name | Business Revenue | Date
1             B1              2000               2013-04-13
2             B2              24000              2014-09-02

I have tried this:

model.objects.filter(date__gte = date.today()).order_by('business_id', '-date')

This is giving me group of businesses having latest business in each group at the top of it. How do I omit the rest of rows? I need some assistance

Paulo Pessoa

Try this:

from django.db.models import Q

group_dict = Model.objects.values('business_id').annotate(max_date=Max('date')).order_by()

params = Q()
for obj in group_dict:
    params |= (Q(business_id__exact=obj['business_id']) & Q(date=obj['max_date']))

qs = Model.objects.filter(params)

This link can help u.

If the values() clause precedes the annotate() clause, any annotations will be automatically added to the result set. However, if the values() clause is applied after the annotate() clause, you need to explicitly include the aggregate column.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Take each first element of the group by

From Dev

get first row for each group

From Java

Pandas dataframe get first row of each group

From Dev

Get a list of first record for each group

From Dev

Get the first occurence of the result in each specified group

From Dev

Get the first occurence of the result in each specified group

From Dev

Get nearest date value group by each item

From Dev

Get the first date and time from within a group

From Java

Get the first element of each tuple in a list in Python

From Dev

Pandas Get First Element of Each Tuple in Cell

From Dev

Get first character of each element of a column in a dataframe

From Dev

How to select first element in each group using jQuery?

From Dev

How to select first element in each group using jQuery?

From Dev

How do I prepend first element of Observable<T> to each group?

From Dev

select data group by distinct date and get first and last data in the group

From Dev

get first occurrence of element in each occurrence of parent element

From Dev

Get most recent date, underscore

From Dev

Get the object with the most recent date

From Dev

Get most recent date, underscore

From Dev

Trying to group by foreign key to get only one row each time that's the most recent effectiveDate

From Dev

Select row with most recent date by group

From Dev

mysql group by keeping the more recent date

From Dev

Select the date of a UserIDs first/most recent purchase

From Dev

First row for each group

From Dev

How to get first row from each group from a table in sqlitedatabase?

From Dev

How to get first record in each group using Linq

From Dev

Google Spreadsheet Queries : How to get the first record of each group?

From Dev

Pandas Dataframe: get average of first rows of each subgroup within a group

From Dev

Google Spreadsheet Queries : How to get the first record of each group?

Related Related

  1. 1

    Take each first element of the group by

  2. 2

    get first row for each group

  3. 3

    Pandas dataframe get first row of each group

  4. 4

    Get a list of first record for each group

  5. 5

    Get the first occurence of the result in each specified group

  6. 6

    Get the first occurence of the result in each specified group

  7. 7

    Get nearest date value group by each item

  8. 8

    Get the first date and time from within a group

  9. 9

    Get the first element of each tuple in a list in Python

  10. 10

    Pandas Get First Element of Each Tuple in Cell

  11. 11

    Get first character of each element of a column in a dataframe

  12. 12

    How to select first element in each group using jQuery?

  13. 13

    How to select first element in each group using jQuery?

  14. 14

    How do I prepend first element of Observable<T> to each group?

  15. 15

    select data group by distinct date and get first and last data in the group

  16. 16

    get first occurrence of element in each occurrence of parent element

  17. 17

    Get most recent date, underscore

  18. 18

    Get the object with the most recent date

  19. 19

    Get most recent date, underscore

  20. 20

    Trying to group by foreign key to get only one row each time that's the most recent effectiveDate

  21. 21

    Select row with most recent date by group

  22. 22

    mysql group by keeping the more recent date

  23. 23

    Select the date of a UserIDs first/most recent purchase

  24. 24

    First row for each group

  25. 25

    How to get first row from each group from a table in sqlitedatabase?

  26. 26

    How to get first record in each group using Linq

  27. 27

    Google Spreadsheet Queries : How to get the first record of each group?

  28. 28

    Pandas Dataframe: get average of first rows of each subgroup within a group

  29. 29

    Google Spreadsheet Queries : How to get the first record of each group?

HotTag

Archive