Here is my mssql code snippet
JavaScript
x
6
1
SELECT
2
Sum((Case When me.status not in ('CLOSED','VOID') and me.pc_cd in ('IK','JM')
3
Then 1 else 0 end)) as current_cd
4
from
5
ccd_pvc me with(nolock)
6
How would i use the and operator with case statement if i write the above statement in sqlalchemy.
I have tried doing this but did not work
JavaScript
1
3
1
case([and_((ccd_pvc.status.in_(['CLOSED', 'VOID']),ccd_pvc.pc_cd.in_(['IK','JM'])),
2
literal_column("'greaterthan100'"))])
3
I have searched through the sqlalchemy documentation but did not find the info on using logical operators with case statement.
The link has some info on this.
Advertisement
Answer
This should get you started:
JavaScript
1
11
11
1
ccd_pvc = aliased(CcdPvc, name="me")
2
3
expr = func.sum(
4
case([(and_(
5
ccd_pvc.status.in_(['CLOSED', 'VOID']),
6
ccd_pvc.pc_cd.in_(['IK', 'JM'])
7
), 1)], else_=0)
8
).label("current_cd")
9
10
q = session.query(expr)
11