SQLAlchemy query not working with functions

Viewed 163

I have a query that I converted into sqlalchemy format. When I log the query and insert into my IDE to test... it works fine.

query= session.query(
            ((func.substr(UserMetrics.START_MONTH, 1, 4)).concat("-").concat(func.substr(UserMetrics.START_MONTH, 5, 6))).label("START_MONTH"),
            UserMetrics.TYPE.label("TYPE"),
            func.avg(UserMetrics_New.SCORE).label("SCORE"))\
        .join(UserMetrics_New,
         and_(UserMetrics.TYPE.in_(('Admin'))))\
        .group_by(((func.substr(UserMetrics.START_MONTH, 1, 4)).concat("-").concat(func.substr(UserMetrics.START_MONTH, 5, 6))),UserMetrics.TYPE)\
        .cte("updated_score")

However, when I run in python... I get the following error:

ORA-00979: not a GROUP BY expression-- still getting this error

It turns out that using func.substr in the select and groupby is causing the issue.

UserMetrics table has the date in yyyy-mm-dd format.. I want to convert that into yyyy-mm format that UserMetrics_New has rows with then do a group by with it.

How can I achieve this?

0 Answers
Related