1

I have a very simple table:

mysql> desc stats;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| entry_id    | int(11)     | NO   | PRI | NULL    | auto_increment | 
| entry_date  | date        | NO   |     | NULL    |                | 
| show_name   | varchar(40) | NO   |     | NULL    |                | 
| month_total | int(11)     | NO   |     | NULL    |                | 
+-------------+-------------+------+-----+---------+----------------+

I want a select statement that will give me the results from the current date as well as the previous day's data in a single statement, so for example, the results would be something like

show_name month_total(Current day) monthly_total(Previous Day)

Is there an easy way to do this within a single sql statement?

3 Answers 3

0

UPDATED: It makes sense to add entry_date to the output

SELECT c.show_name, 
       c.month_total current_month_total, 
       p.month_total prev_month_total
  FROM stats c INNER JOIN
       stats p ON p.entry_date = c.entry_date - INTERVAL 1 DAY

Assuming that you have sample data like

+----------+------------+-----------+-------------+
| entry_id | entry_date | show_name | month_total |
+----------+------------+-----------+-------------+
|        1 | 2013-03-07 | Name1     |          10 |
|        2 | 2013-03-08 | Name2     |          20 |
|        3 | 2013-03-09 | Name3     |          30 |
|        4 | 2013-03-10 | Name4     |          40 |
+----------+------------+-----------+-------------+

The output of the query is

+-----------+------------+---------------------+------------------+
| show_name | entry_date | current_month_total | prev_month_total |
+-----------+------------+---------------------+------------------+
| Name2     | 2013-03-08 |                  20 |               10 |
| Name3     | 2013-03-09 |                  30 |               20 |
| Name4     | 2013-03-10 |                  40 |               30 |
+-----------+------------+---------------------+------------------+

Here is a sqlfiddle example

If you need the output for a specific date or for an interval of dates just add a WHERE clause.
For today

...
WHERE c.entry_date = CURDATE();

From 03/09 to 03/10

...
WHERE c.entry_date BETWEEN '2013-03-09' AND '2013-03-10'
2
  • shouldn't it be the other way around? I mean your previous day is actually next day, isn't it?
    – jurgenreza
    Mar 8, 2013 at 21:33
  • @xxedgexx Do you need more help with your question?
    – peterm
    Mar 9, 2013 at 2:07
0

If by current day you mean today, use SUBDATE and CURDATE functions and a subquery in your select statement:

SELECT 
    show_name, 
    month_total AS current_day_total, 
    (SELECT month_total FROM stats WHERE entry_date = SUBDATE(CURDATE(), 1) LIMIT 1) AS previous_day_total
FROM stats
WHERE entry_date = CURDATE();

see fiddle.

If you want this for everyday and not just the current day, go with join.

0

assuming this test data:

+----------+------------+-----------+-------------+
| 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 |
+----------+------------+-----------+-------------+

If you have multiple entries per day for different names, than this should work well:

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 |
+-----------+------------+-------------------------+--------------------------+

If the there are even multiple entries per day and name than this would combine their values: (see line one of the result table for test1 - 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 |
+-----------+------------+-------------------------+--------------------------+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.