Opened 5 years ago

Last modified 5 years ago

#29976 closed Uncategorized

How to perform Group by on Union of two models — at Initial Version

Reported by: Sandeep Owned by: nobody
Component: Database layer (models, ORM) Version: 2.1
Severity: Normal Keywords: Union and annotate(Count(''))
Cc: k.sandeep44@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have two models like this
class Story(models.Model):

name = models.TextField(db_column='NAME') # Field name made lowercase.
status = models.TextField(db_column='STATUS') # Field name made lowercase.


class Bug(models.Model):

name = models.TextField(db_column='NAME') # Field name made lowercase.
status = models.TextField(db_column='STATUS') # Field name made lowercase.


I would like to get count of Unique status from above two tables

Query: Select new_table.status, Count(new_table.name) from (select status, name from Story Union select status, name from Bug) new_table group by new_table.status

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top