简体   繁体   中英

SqlAlchemy Case with multiple conditions

I am trying to do something like this:

x = db_session.query(
    Candidate,
    func.count(case([(Jobs.interview_type == 'PHONE_SCREEN' and Jobs.interview_type == 'INCLINED' , 1)])),
    func.count(case([(Jobs.interview_type == 'PHONE_SCREEN' and Jobs.interview_type == 'NOT_INCLINED', 1)])),
    func.count(case([(Jobs.interview_type == 'IN_HOUSE', 1)])),
    func.count(case([(Jobs.interview_type == 'EVALUATION', 1)]))
    ).\
    join(Jobs, Jobs.candidate_id == Candidate.candidate_id).\
    filter(Candidate.candidate_id == '6236738').\
    first()

However its not picking up the second condition in case . Is this possible?

I got it working with and_ but its not giving the right answer

func.count(case([(and_(Jobs.interview_type == 'PHONE_SCREEN', Jobs.interview_type == 'INCLINED'), 1)])),

should return 2, but its returning 0

You need to use sqlalchemy.and_ instead of the and operator:

and_(Jobs.interview_type == 'PHONE_SCREEN',
     Jobs.interview_type == 'INCLINED')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM