Skip to content
Advertisement

SQLAlchemy group by day in timestamp column

I have an ORM (Model) defined in SQLAlchemy as below:

I’m planning to get the sum of all views in each day and tried to group the results based on their end_to. I have used the following query in sqlalchemy:

But this query throws this error:

I am using the timestamp in my model and I don’t plan to change it because of some reason. The only thing I can do is modify the query. The SQLAlchemy is connected to AWS Redshift.

Advertisement

Answer

If you wanted to group by the number of views per day in Postgresql the query would look like this (WHERE clause omitted):

The trick to dealing with the timestamp is to cast it to the date type, which truncates the value to the date part. In SQLAlchemy the equivalent code would be

Resulting in values like

The equivalent SQLAlchemy 2.0 style query would be

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement