title: SQLite, CONDITIONAL SUM
body: <pre><code>table_payments
  
  id  id_order   quantity
  1.    1.         50
  2.    1.         150   
  3.    2.         100
  4.    2.          20
  
  table_order_details
  
  id.  id_order    total
  1.    1.          300
  2.    2.          200
  </code></pre>
  
  <p>I need a query to add the payments and subtract the total.</p>
  
  <p>Example:</p>
  
  <pre><code>50+150=300-200=100
  100+20=200-120=80
  </code></pre>
  
  <p>result of the query: 180</p>
  
  <p>The query adds the payments and subtracts the total and results in the debit.</p>
  
  <p>Thank danny117</p>
  
  <pre><code>SELECT
  SUM(agenda_detalles.agendadetalle_precio_total) - (
  
  SELECT
  SUM(agenda_pagos.agenda_pago_cantidad)
  FROM
  agenda
  JOIN agenda_pagos
  ON agenda.agenda_id = agenda_pagos.agenda_id
  WHERE agenda_pagado = 1
  
  ) AS adeudo
  FROM
  agenda
  JOIN agenda_detalles
  ON agenda.agenda_id = agenda_detalles.agendadetalle_agenda_id
  WHERE agenda_pagado = 1
  </code></pre>
  
  <p>I solved it in the following way</p>