SQL "IF" in sqlalchemy?

7 views
Skip to first unread message

Diego Woitasen

unread,
Nov 28, 2009, 1:54:29 PM11/28/09
to sqlal...@googlegroups.com
Does sqlalchemy support SQL IF? For example:

select date, if(proxy_user_id <> 1, count(distinct address_id), 0)
from table group by date;

I've solved this using literal SQL in Query() parameters but may be I
can do it using ORM.


--
Diego Woitasen
XTECH

Michael Bayer

unread,
Nov 28, 2009, 1:56:41 PM11/28/09
to sqlal...@googlegroups.com
we support CASE via case() which will get you there just as well.
> --
>
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
>
>

Diego Woitasen

unread,
Nov 29, 2009, 3:10:36 PM11/29/09
to sqlal...@googlegroups.com
2009/11/28 Michael Bayer <mik...@zzzcomputing.com>:
I discovered that func.if_() works.

count_ip = func.if_(MiniAccess.proxy_user_id == dash_id,

func.count(MiniAccess.address_id.distinct()), 0)

I looks like func.foo() are translated to SQL FOO(), with the same arguments.



--
Diego Woitasen
XTECH

Alex Brasetvik

unread,
Nov 30, 2009, 1:45:31 PM11/30/09
to sqlal...@googlegroups.com

On Nov 29, 2009, at 21:10 , Diego Woitasen wrote:

I discovered that func.if_() works.

count_ip = func.if_(MiniAccess.proxy_user_id == dash_id,

func.count(MiniAccess.address_id.distinct()), 0)

Note that IF is a MySQL-ism, whereas CASE works on most databases.


I looks like func.foo() are translated to SQL FOO(), with the same arguments.

The docs rarely lie - http://www.sqlalchemy.org/docs/06/sqlexpression.html#functions :-)

--
Alex Brasetvik
Reply all
Reply to author
Forward
0 new messages