Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Add the date condition into the <code>WHERE</code>-statement and the exclusion of your <code>SUM</code> as a <code>HAVING</code>-statement. It should then look like this:</p> <pre><code>SELECT cms_module_ladu_batch.id AS batch_id, cms_module_ladu_batch.date_changed AS date_ch, kogused.kogus AS kogus, SUM(kogused.kogus) AS total FROM cms_module_ladu_batch LEFT JOIN kogused ON cms_module_ladu_batch.id = kogused.bid WHERE t2 != 'Sildid' AND ( ( cms_module_ladu_batch.date_changed &lt; DATE_SUB(NOW(), INTERVAL 1 MONTH) AND SUM(kogused.kogus) &gt; 0 ) OR cms_module_ladu_batch.date_changed &gt; DATE_SUB(NOW(), INTERVAL 1 MONTH) ) GROUP BY batch_id; </code></pre> <p>Alternatively... use a UNION-Statement to glue two SELECTS together. </p> <pre><code>SELECT cms_module_ladu_batch.id AS batch_id, cms_module_ladu_batch.date_changed AS date_ch, kogused.kogus AS kogus, SUM(kogused.kogus) AS total FROM cms_module_ladu_batch LEFT JOIN kogused ON cms_module_ladu_batch.id = kogused.bid WHERE t2 != 'Sildid' AND cms_module_ladu_batch.date_changed &lt; DATE_SUB(NOW(), INTERVAL 1 MONTH) GROUP BY batch_id HAVING total &gt; 0 UNION SELECT cms_module_ladu_batch.id AS batch_id, cms_module_ladu_batch.date_changed AS date_ch, kogused.kogus AS kogus, SUM(kogused.kogus) AS total FROM cms_module_ladu_batch LEFT JOIN kogused ON cms_module_ladu_batch.id = kogused.bid WHERE t2 != 'Sildid' AND cms_module_ladu_batch.date_changed &gt; DATE_SUB(NOW(), INTERVAL 1 MONTH) GROUP BY batch_id; </code></pre> <p>Try both, see what fits best for your solution.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload