Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL Conditions from Multiple Rows
    primarykey
    data
    text
    <p>I have been trying to calculate a table using a <code>SELECT</code> statement. I have a table like this:</p> <pre><code>-------------------------------------------------------------- AgentID | Date | Incurred | FallOffDate ============================================================== kegomez | 2012-11-19 | 2.0 | 2013-11-19 kegomez | 2012-11-24 | 0.5 | 2013-11-24 kegomez | 2013-01-21 | 2.0 | 2014-01-21 kegomez | 2013-08-18 | 2.0 | 2014-08-18 </code></pre> <p>I was trying to do the calculations on during the select and possibly create a view but have no luck so far. In the end the table will look like this.</p> <pre><code>-------------------------------------------------------------- AgentID | Date | Incurred | 90 | 180 | Total | FallOffDate ============================================================== kegomez | 2012-11-19 | 2.0 | 2.0 | 2.0 | 2.0 | 2013-11-19 kegomez | 2012-11-24 | 0.5 | 0.5 | 0.5 | 2.5 | 2013-11-24 kegomez | 2013-01-21 | 2.0 | 1.0 | 0.0 | 2.5 | 2014-01-21 kegomez | 2013-08-18 | 2.0 | 2.0 | 2.0 | 4.5 | 2014-08-18 </code></pre> <p>The total column uses values from the previous row to calculate its values. For example the date in row 4 will need to reference the date in row 3 to see if the date is greater. Would I need to try this with a subquery? How this will eventually work is that every 90 days up to 180 days the agent will loose 1 point if no more are incurred. Thus the reason why I need to reference other rows. If it helps this data is currently in Excel but is getting too large to manage and we need to move it over to something that performs better.</p> <pre><code>SELECT AgentID, Date, Incurred, @90 := IF(Date&lt;=CURDATE()-90 AND @r=0, Incurred-1.0, IF(Difference&gt;90, Incurred-1, Incurred)) AS 90Day, @180 := IF(Date&lt;=CURDATE()-90 AND @r=0, Incurred-1.0, IF(Difference&gt;180, Incurred-2, @90)) AS 180Day, @Total := IF(@180&lt;0,0,IF(FallOffDate&lt;=CURDATE(),0, @180)) AS Total, FallOffDate FROM (SELECT mo.AgentID, mo.Incurred, FallOffDate, @r AS LEAD_date, DATEDIFF(@r,Date) AS Difference, (@r := Date) AS Date FROM ( SELECT m.* FROM ( SELECT @_date = NULL ) VARIABLE, attendance m ORDER BY AgentID, Date DESC ) mo WHERE (CASE WHEN @_date IS NULL OR @_date &lt;&gt; date THEN @r := NULL ELSE NULL END IS NULL) AND (@_date := date) IS NOT NULL) T ORDER BY AgentID, Date; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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