title: SQLAlchemy MySQL IF Statement
body: <p>I'm in the middle of converting an old legacy PHP system to Flask + SQLAlchemy and was wondering how I would construct the following:</p>
  
  <p>I have a model:</p>
  
  <pre><code>class Invoice(db.Model):
     paidtodate = db.Column(DECIMAL(10,2))
     fullinvoiceamount = db.Column(DECIMAL(10,2))
     invoiceamount = db.Column(DECIMAL(10,2))
     invoicetype = db.Column(db.String(10))
     acis_cost = db.Column(DECIMAL(10,2))
  </code></pre>
  
  <p>The query I need to run is:</p>
  
  <pre><code>SELECT COUNT(*) AS the_count, sum(if(paidtodate&gt;0,paidtodate,if(invoicetype='CPCN' or invoicetype='CPON' or invoicetype='CBCN' or invoicetype='CBON' or invoicetype='CPUB' or invoicetype='CPGU' or invoicetype='CPSO',invoiceamount,
  fullinvoiceamount))) AS amount,
  SUM(acis_cost) AS cost, (SUM(if(paidtodate&gt;0,paidtodate,invoiceamount))-SUM(acis_cost)) AS profit FROM tblclientinvoices
  </code></pre>
  
  <p>Is there an SQLAlchemyish way to construct this query? - I've tried googling for Mysql IF statments with SQlAlchemy but drew blanks.</p>
  
  <p>Many thanks!</p>