I want to generate this SQL:
SELECT
SUBSTR(domain_name, 1, 1) as first_letter,
COUNT(domain_name) as counted
FROM domain
GROUP BY first_letter;
The closest I can get to it is this:
dbSession.query(
sqlalchemy.func.count( model.Domain.domain_name ).label('counted'),
sqlalchemy.func.substr( model.Domain.domain_name, 1, 1 ).label('first_letter'),
).group_by(
sqlalchemy.func.substr( model.Domain.domain_name, 1, 1 )
)
I tried using this as a the group by:
sqlalchemy.sql.expression.literal('first_letter')
however postgres doesn't like the group by coming in as a bind param.
is there another way to construct the intended query, or do I have to use 2 substring operations (or a subquery)?