I have looked up a few different answers to this question and can't seem to get the query to work properly.
Here is my table has the columns user, weekNo, salesTotalYTD.
I am currently pulling these out and grouping them by week like so:
+------+--------+---------------+
| user | weekNo | salesTotalYTD |
+------+--------+---------------+
|Jared | 1 | 200 |
+------+--------+---------------+
| Jim | 1 | 50 |
+------+--------+---------------+
|Jared | 2 | 30 |
+------+--------+---------------+
| Jim | 2 | 100 |
+------+--------+---------------+
What I am trying to do but cannot accomplish is the following:
+------+--------+---------------+
| user | weekNo | salesTotalYTD |
+------+--------+---------------+
|Jared | 1 | 200 |
+------+--------+---------------+
| Jim | 1 | 50 |
+------+--------+---------------+
|Jared | 2 | 230 |
+------+--------+---------------+
| Jim | 2 | 150 |
+------+--------+---------------+
This is the query that I have working for the first pass but every pass after that is wrong:
SET @runtot:=0
SELECT
salesTotalYTD,
user,
(@runtot := @runtot + salesTotalYTD) AS rt
FROM weeksAndSalesmantbl
GROUP BY user, weekNo
ORDER BY (CASE WHEN weekNo = 52 THEN 0 ELSE 1 END) ASC, weekNo, user ASC
Updated
Updated code courtesy of Tim but returning error:
$assignments = "
SELECT
t1.user,
t1.weekNo,
(SELECT SUM(t2.salesTotalYTD) FROM weeksAndSalesmantbl t2
WHERE t2.user = t1.user AND t2.weekNo <= t1.weekNo) AS salesTotalYTD
FROM weeksAndSalesmantbl t1
ORDER BY
t1.weekNo,
t1.user";
$salesTotalSalesManCumulative = [];
$assignmentsqry = mysqli_query($db,$assignments);
if (!$assignmentsqry) {
printf("Error: %s\n", mysqli_error($db));
exit();
}
while ($row = mysqli_fetch_array($assignmentsqry)) {
$float = floatval($row['salesTotalYTD']);
$float = round($float,2);
array_push($salesTotalSalesManCumulative,$float);
}