Pulling info from the same table in different orders and quantities using MySQL
-
17-06-2021 - |
Pergunta
First time post. As an example, say I have a table with 12 entries;
+--------+------------+ | name | date | +--------+------------+ | P1 | 2011-12-01 | | P2 | 2012-01-01 | | P3 | 2012-02-01 | | P4 | 2012-02-01 | | P5 | 2012-02-01 | | P6 | 2012-05-01 | | P7 | 2012-04-01 | | P8 | 2012-05-01 | | P9 | 2012-05-01 | | P10 | 2012-06-01 | | P11 | 2012-06-01 | | P12 | 2012-03-01 | +--------+------------+
I have grouped the information within the table by the date, kept a count of all of the id's (not shown) and created a running total. Which I have implemented as so;
(SELECT date, c as count, (@s := @s + c) as run_total FROM (SELECT D1.date, Count(D1.person_id) as c, @s := 0 FROM table D1 GROUP BY month(D1.date) ORDER BY D1.date) as T1);
Output;
+------------+-------+-----------+ | date | count | run_total | +------------+-------+-----------+ | 2011-12-20 | 1 | 1 | | 2012-01-31 | 1 | 2 | | 2012-02-20 | 3 | 5 | | 2012-03-05 | 1 | 6 | | 2012-04-20 | 1 | 7 | | 2012-05-20 | 3 | 10 | | 2012-06-01 | 2 | 12 | +------------+-------+-----------+
Good so far. This is where I loose it. How can I extract the most recent 4 entries and get something that looks like this??? where the dates remain in ascending order and more importantly the run_total stays the same;
-----What I am trying to achieve-----
---------------------------------- | 2012-03-05 | 1 | 6 | | 2012-04-20 | 1 | 7 | | 2012-05-20 | 3 | 10 | | 2012-06-01 | 2 | 12 | +------------+-------+-----------+
-------------------------------------
In trying to work this out I created this;
(SELECT date, c as count, (@s := @s + c) as run_total FROM (SELECT D1.date, Count(D1.person_id) as c, @s := 0 FROM table D1 GROUP BY month(D1.date) ORDER BY D1.date desc limit 4) as T1) ORDER BY date;
But I did not get the desired outcome;
+------------+-------+-----------+ | date | count | run_total | +------------+-------+-----------+ | 2012-03-05 | 1 | 1 | | 2012-04-20 | 1 | 2 | | 2012-05-20 | 3 | 5 | | 2012-06-01 | 2 | 7 | +------------+-------+-----------+
The dates are in order but the run_totals are all messed up. 1 is supposed to be 6, 2 -> 7, 5 -> 10, and 7 -> 12 as shown earlier.
Again, what I am trying to achieve is;
---------------------------------- | 2012-03-05 | 1 | 6 | | 2012-04-20 | 1 | 7 | | 2012-05-20 | 3 | 10 | | 2012-06-01 | 2 | 12 | +------------+-------+-----------+
Solução
SELECT * FROM
(SELECT *
FROM (SELECT date, count, (@s := @s + c) as run_total
FROM
(SELECT D1.date, Count(D1.person_id) as c, @s := 0
FROM table D1
GROUP BY month(D1.date)
ORDER BY D1.date) as T1) tmp
ORDER BY `date` DESC
LIMIT 4) tmp2
ORDER BY `date` ASC
It takes the 4 most recent records of your query, then reorders them by date.
The totals remain unchanged.
Outras dicas
(SELECT date, c as count, (@s := @s + c) as run_total
FROM
(SELECT D1.date, Count(D1.person_id) as c, @s := 0
FROM table D1
GROUP BY month(D1.date)
ORDER BY D1.date ASC) as T1) LIMIT 4;