OK, after 2 days of trying and searching creating a post in the google 
groups leads to rephrasing the problem. And this often leads to finding the 
solution:

I managed to do it this way:

   MyTable.query.with_entities(MyTable.columnB).distinct().all()

Problem solved...

Am Sonntag, 8. Oktober 2017 09:27:49 UTC+2 schrieb Cornelius Kölbel:
>
> Hi,
>
> I have a table with many columns (columnA, columnB, columnC) and I want to 
> know, how many different entries in columnB are.
> I would do such an SQL query:
>
>     select distinct columnB from myTable;
>
> I see there is also ``distinct()`` in SQL Alchemy.
>
> However - I am using Class defined ORM like this:
>
>     class MyTable(db.Model):
>          __tablename__ = "myTable"
>         columnA = db.Column(...)
>         columnB = db.Column(...)
>         columnC = db.Column(...)
>
> Thus, I am querying like this:
>
>
>     MyTable.query.all()
>
> The problem seems to be that I was not able to figure out a way to reduce 
> the columns, e.g. when I add a ``distinct``
>
>
>     MyTable.query.distinct.all()
>
> The distinct function will always use **all** columns.
> I also tried ``load_only`` with no success.
>
> I understand, I could define a class, containing only the required column, 
> then I am told, I should just some ``extend_existing``.
>
> What is the required way to use  ``distinct`` in a class based query?
>
> Thanks a lot 
> Cornelius
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to