I have the follwoing table and want to compute the sum
of true and group_by
by date
| Date | Value1 | Value2 |
|:-----------|------------:|:------------:|
| 2019-12-05 | 5.5 | true|
| 2019-12-05 | 4.5 | true|
| 2019-12-05 | 6.5 | false|
| 2019-12-05 | 8.5 | false|
| 2019-12-05 | 2.5 | true|
| 2019-12-06 | 3.5 | true|
| 2019-12-06 | 5.5 | true|
| 2019-12-06 | 56.5 | true|
| 2019-12-06 | 8.5 | true|
| 2019-12-06 | 99.5 | false|
| ... | ... | ... |
my query on a db.Model by a classmethod:
return db.session.query(cls.Date, func.count(cast(cls.Value2, sqlalchemy.Integer)).filter(cls.Value2== True).label("Count True Value2")) \
.group_by(cls.Date)\
.order_by("Date")
Result should be
| Date | Value2 |
|:-----------|------------:|
| 2019-12-05 | 3 |
| 2019-12-06 | 4 |
EDIT does not work either
return db.session.query(cls.Date, func.sum(case([(cls.Value2== True, 1)], else_=0).label('Value2'))) \
.filter(cls.Date== current_date.strftime("%Y-%m-%d")) \
.group_by(cls.Date)\
.order_by("Date")