Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python peewee - how to use distinct

I'm trying to get this code working with peewee:

distinct_list = QSales.select(QSales.account, QSales.tax_code).distinct().where(QSales.trans_num == 3717)
print distinct_list

but the print command result is:

<class '__main__.QSales'> SELECT DISTINCT t1.`Account`, t1.`Tax_Code` FROM `q_sales` AS t1 WHERE (t1.`Trans_#` = %s) [3717]

running the above select statement in MySQL editor (copy the print result to the editor) returns correct result.

I also tried:

distinct_list = QSales.select(fn.Distinct(QSales.account, QSales.tax_code)).where(QSales.trans_num == 3717)

but got the same result

What am I doing wrong?

Thank you.

like image 288
Erans Avatar asked Jul 11 '13 15:07

Erans


People also ask

How do I use peewee in Python?

Peewee is a Python ORM (Object-Relational Mapping) library which supports SQLite, MySQL, PostgreSQL and Cockroach databases. This tutorial will help you to understand how to insert a new record, delete a record, create an index, etc., with the help of Peewee.

What does distinct mean in Python?

Distinct keyword is used in DB and it is to return record set only with distinct elements for that particular column.

Is select distinct fast?

Most of the SELECT DISTINCT queries will perform exactly as fast as their simple SELECT counterparts, because the optimizer will do away with the step necessary for eliminating duplicates.


1 Answers

Sleeping over it, I realized that that code should be as follows:

distinct_list = QSales.select(QSales.account, QSales.tax_code).distinct().where(QSales.trans_num == 3717)
for item in distinct_list:
    print item.account
    print item.tax_code

This is closed now. Thank you.

like image 140
Erans Avatar answered Sep 21 '22 14:09

Erans