简体   繁体   English

sqlalchemy 按孩子月/年计算唯一父母

[英]sqlalchemy count unique parents by children month/year

This is kind of hard for me to explain but I'm trying to create a query to count how many unique parents there are by month depending on if they have children with birth dates in that month.这对我来说有点难以解释,但我正在尝试创建一个查询来计算每月有多少唯一父母,具体取决于他们是否有孩子在该月出生。 So I have a parent and child model that have a relationship.所以我有一个父母和孩子 model 有关系。 Each child has a birthdate and parent id and each parent can have multiple children within the same month of their other children.每个孩子都有一个出生日期和父母 ID,每个父母可以在其他孩子的同一个月内有多个孩子。 If a Parent has 10 children in the month of January, I only want them to be counted once.如果父母在一月份有 10 个孩子,我只希望他们被计算一次。 I can count how many children have birthdates by month like so:我可以按月计算有多少孩子的生日,如下所示:

children_query = db.query(func.count(Child.id).label('Children'),
                            func.extract('year', Child.birth_date),
                            func.extract('month', Child.birth_date))\
                            .group_by(func.extract('year', Child.birth_date),
                                    func.extract('month', Child.birth_date)).all()

But im having trouble wrapping my head around creating a query to count how many parents have children with birthdates in each month.但是我很难围绕创建一个查询来计算每个月有多少父母有孩子的生日。 Basically the result I need needs to show there are 10 unique parents with children in Jan, 11 in Feb, 5 in Mar, etc..基本上我需要的结果需要显示 1 月有 10 个独特的父母有孩子,2 月 11 日,3 月 5 日,等等。

Is there a way to remove duplicate parent_ids and count by parent_id?有没有办法删除重复的 parent_ids 并按 parent_id 计数?

Here are the models这里是模型

class Parent(Base):
    id = Column(Integer, primary_key=True)
    children = relationship('Child', backref="parent")

class Child(Base):
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))
    birth_date = Column(DateTime, nullable=False)

I think this query does what you want:我认为此查询可以满足您的要求:

q = (session.query(sa.func.extract('YEAR', Child.birth_date).label('year'),
                   sa.func.extract('MONTH', Child.birth_date).label('month'),
                   sa.func.count(sa.func.distinct(Child.parent_id)))
            .group_by('year', 'month')
            .order_by('year', 'month'))

it generates this SQL (on Postgres)它生成这个 SQL (在 Postgres 上)

SELECT EXTRACT(YEAR FROM children.birth_date) AS year, 
       EXTRACT(MONTH FROM children.birth_date) AS month,
       count(distinct(children.parent_id)) AS count_1 
  FROM children 
  GROUP BY year, month 
  ORDER BY year, month

The trick is using COUNT(DISTINCT...) .诀窍是使用COUNT(DISTINCT...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM