Moshe C. wrote:
> How do I write an expression (in sqlalchemy 0.4.6) that will generate
> the following expression:
>
> select count(distinct  field1, field2)   from tableA;
>
> I know how to do it for one field and I know how to do it with no
> count using distinct=True, but not this combination.
>   

What database are you using? I have not heard of a dialect that accepts
multiple columns in a COUNT() function. This seems to work on 0.4.6, but
it is sheer luck and very fragile:
>>> print select([func.count(tableA.c.field1.distinct(),
tableA.c.field2)], from_obj=tableA)
SELECT count(DISTINCT "tableA".field1, "tableA".field2) AS count_1
FROM "tableA"

The "standard" way is to use a subquery like so:
select count(*) from (select distinct field1, field2 from tableA) anon

which you can do via:
>>> print select([func.count(text("*"))],
from_obj=select([tableA.c.field1, tableA.c.field2], distinct=True))
SELECT count(*) AS count_1
FROM (SELECT DISTINCT "tableA".field1 AS field1, "tableA".field2 AS field2
FROM "tableA")

Hope it helps,
-Conor

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to