I have a question for Flask ORM query statement. Calculating Daily Statistics

Asked 2 years ago, Updated 2 years ago, 140 views

First of all, the point of the question is to calculate daily statistics based on DB data You are about to import it.

I'm trying to implement it through the ORM query statement method, but the logic is not well implemented, so I'd like to ask you a question.

In the DB, Y-m-d H:M:S (2021-05-13 12:11:10) is saved through DatetimeField.

today_date = datetime.now().date()
today_count = testdb.query.filter(testdb.session_time ~~)

While looking for an implementation method, we have considered using the >= operator or <=, but this makes it impossible to count the exact date of the day, so I wonder how to get the number of data for that day in a daily statistic.

python sqlalchemy flask orm django

2022-09-20 16:26

1 Answers

I don't know if it'll help... If it's raw, for example, MySQL, it's handled in this way.

select
    date_format(tbl_foo.col_dt, '%Y-%m-%d') as ymd,
    count(*) as counts
from tbl_foo
group by date_format(tbl_foo.col_dt, '%Y-%m-%d')

The key is to convert datetime-type data to the 'date' level and use it for grouping and aggregation. There must be several ways to handle this in SQLAlchemy. If you understand the basic concept, try it.


2022-09-20 16:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.