このテストデータを仮定すると:
+----------+------------+-----------+-------------+
| entry_id | entry_date | show_name | month_total |
| 1 | 2013-03-07 | test1 | 1 |
| 2 | 2013-03-07 | test2 | 11 |
| 3 | 2013-03-08 | test1 | 2 |
| 4 | 2013-03-08 | test2 | 22 |
| 5 | 2013-03-08 | test3 | 222 |
| 6 | 2013-03-09 | test1 | 3 |
| 7 | 2013-03-09 | test2 | 33 |
| 8 | 2013-03-07 | test1 | 5 |
+----------+------------+-----------+-------------+
異なる名前で 1 日に複数のエントリがある場合、これはうまく機能するはずです。
SELECT c.show_name, c.entry_date,
c.month_total current_day_month_total,
IFNULL(p.month_total,0) previous_day_month_total
FROM test.stats c LEFT JOIN test.stats p
ON p.entry_date = c.entry_date - INTERVAL 1 DAY
AND c.show_name = p.show_name
GROUP BY c.entry_date, c.show_name;
+-----------+------------+-------------------------+--------------------------+
| show_name | entry_date | current_day_month_total | previous_day_month_total |
| test1 | 2013-03-07 | 1 | 0 |
| test2 | 2013-03-07 | 11 | 0 |
| test1 | 2013-03-08 | 2 | 1 |
| test2 | 2013-03-08 | 22 | 11 |
| test3 | 2013-03-08 | 222 | 0 |
| test1 | 2013-03-09 | 3 | 2 |
| test2 | 2013-03-09 | 33 | 22 |
+-----------+------------+-------------------------+--------------------------+
1 日と名前ごとに複数のエントリがある場合は、それらの値を結合します: (test1 の結果テーブルの 1 行目を参照 - id1:1 + id8:5 = 6)
SELECT c.show_name, c.entry_date,
c.month_total current_day_month_total,
IFNULL(p.month_total,0) previous_day_month_total
FROM (SELECT show_name, entry_date, SUM(month_total) month_total
FROM test.stats
GROUP BY entry_date, show_name) c
LEFT JOIN
(SELECT show_name, entry_date, SUM(month_total) month_total
FROM test.stats
GROUP BY entry_date, show_name) p
ON p.entry_date = c.entry_date - INTERVAL 1 DAY
AND c.show_name = p.show_name;
+-----------+------------+-------------------------+--------------------------+
| show_name | entry_date | current_day_month_total | previous_day_month_total |
| test1 | 2013-03-07 | 6 | 0 |
| test2 | 2013-03-07 | 11 | 0 |
| test1 | 2013-03-08 | 2 | 6 |
| test2 | 2013-03-08 | 22 | 11 |
| test3 | 2013-03-08 | 222 | 0 |
| test1 | 2013-03-09 | 3 | 2 |
| test2 | 2013-03-09 | 33 | 22 |
+-----------+------------+-------------------------+--------------------------+