Closed KMaster-117 closed 1 year ago
I wish to group_by and aggregate the data based on a transformed field. For example, in this code snippet:
class Table(models.Model): id = fields.BigIntField(pk=True, description="key") created = fields.DatetimeField(auto_now_add=True, description="created_time", null=True) sid = fields.BigIntField(description="space_id") class Meta: abstract = True ordering = ["-created"] class SpaceRollupModel(Table): dev_type_id = fields.BigIntField(description="dev_type_id ") dev_id = fields.BigIntField(description="dev_id") energy_item_id = fields.BigIntField(description="energy_item_id ") value = fields.FloatField(description="value ") status = fields.CharField(max_length=20, description="status ") unit = fields.CharField(max_length=128, description="unit ") class Meta: db = "vse_history_db" table = "tbl_space_rollup" indexes = ("sid", "dev_type_id", "dev_id", "energy_item_id") table_description = "table" SpaceRollupModel.annotate(total=Coalesce(Sum("value"), 0),created=RawSQL("created::date")).filter( sid=sid, dev_type_id=data.dev_type_id, energy_item_id=data.energy_id, created__lte=end_date, created__gte=start_date).group_by("created").order_by("created").values("created", "total")
I am unable to achieve the desired effect of grouping by the transformed field created::date using the group_by function.
I'm sorry, I realized that I should use .annotate after filter, and rename the field to another field name (e.g., time=RawSQL("created::date")), and it worked successfully.
I wish to group_by and aggregate the data based on a transformed field. For example, in this code snippet:
I am unable to achieve the desired effect of grouping by the transformed field created::date using the group_by function.