0

我目前的项目是根据下表生成支出报告。完整的报告是按特许经营的零售类别显示用户的支出 $ 细分,例如

用户 1

零售类别 1:20 美元

零售类别 2:30 美元

零售类别 3:35 美元

用户 2

零售类别 1:10 美元

零售类别 2:15 美元

零售类别 3:5 美元

以下是表格:

class User(models.Model):
    id_user = models.AutoField(primary_key = True)

class Franchises(models.Model):
    id_franchise = models.AutoField(primary_key=True)
    retail_category = models.IntegerField(default=99)  # values are 1 to 13

class Stores(models.Model):
    id_store = models.AutoField(primary_key=True)
    franchise = models.ForeignKey(Franchises, db_column='id_franchise')

class Receipts(models.Model):
    id_receipt = models.AutoField(primary_key=True)
    store = models.ForeignKey(Stores, db_column='id_store')
    user = models.ForeignKey(User, db_column='id_user')
    grand_total = models.DecimalField(max_digits=19, decimal_places=4)

我会很感激任何原始的 mysql 或 django 模型查询。谢谢,

4

1 回答 1

0

希望我将您的 django 模式规范正确地转换为原始 mysql:

SELECT user, retail_category, SUM(grand_total)
FROM Receipts
INNER JOIN Stores ON Receipts.store = Stores.id_store
INNER JOIN Franchises ON Stores.franchise = Franchises.id_franchise
GROUP BY user, retail_category;
于 2012-06-26T00:42:24.453 回答