TheDataGuy
TheDataGuy

Reputation: 3118

mysql - calculate difference between two dates

I have a table which looks like below.

CREATE TABLE `table_growth` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `timestamp` datetime DEFAULT CURRENT_TIMESTAMP,
  `table_name` varchar(50) DEFAULT NULL,
  `rows` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=184 DEFAULT CHARSET=utf8

Example of rows in the table:

+-----+---------------------+--------------------------+-------+
| id  | timestamp           | table_name               | rows  |
+-----+---------------------+--------------------------+-------+
| 110 | 2019-03-01 06:00:00 | attachments              |   640 |
| 111 | 2019-03-01 06:00:00 | contacts                 |     0 |
| 112 | 2019-03-01 06:00:00 | copy_menuitem_options    |  3038 |
| 113 | 2019-03-01 06:00:00 | copy_menuitem_suboptions |  9779 |
| 114 | 2019-03-01 06:00:00 | copy_menuitems           | 12118 |
| 115 | 2019-03-02 06:00:00 | attachments              |   638 |
| 116 | 2019-03-02 06:00:00 | contacts                 |     0 |
| 117 | 2019-03-02 06:00:00 | copy_menuitem_options    |  3039 |
| 118 | 2019-03-02 06:00:00 | copy_menuitem_suboptions |  9789 |
| 119 | 2019-03-02 06:00:00 | copy_menuitems           | 12128 |
+-----+---------------------+--------------------------+-------+

I want to calculate the diff(rows) between 2 days. like date(timestamp)='2019-03-02' - date(timestamp)='2019-03-01'

Expected results

table_name              | Rows Diff
------------------------------
attachments             | 2
contacts                | 0           
copy_menuitem_options   | 1
copy_menuitem_suboptions| 10 
copy_menuitems          | 10 

I tried these queries, but somewhere its failing.

 SELECT x.table_name
     , (y.rows-x.rows)as diff
  FROM dbadmin.table_growth x
  JOIN dbadmin.table_growth y
    ON y.id = x.id
   AND DATE(y.timestamp) = '2019-03-02' 
 WHERE DATE(x.timestamp) = '2019-03-01';

select x.table_name, (y.rows - x.rows) as doff
from table_growth x join
table_growth y on y.id=x.id and DATE(y.timestamp) = '2019-03-02' 
WHERE DATE(x.timestamp) = '2019-03-01';

Upvotes: 0

Views: 49

Answers (3)

Gordon Linoff
Gordon Linoff

Reputation: 1270993

If you have only one row per date, then this might be the fastest approach:

SELECT g.table_name,
       SUM(CASE WHEN DATE(g.timestamp) = '2019-03-02'
                THEN g.rows 
                WHEN DATE(g.timestamp) = '2019-03-01'
                THEN -g.rows
                ELSE 0
           END) as diff
FROM dbadmin.table_growth g
WHERE g.timestamp >= '2019-03-01' AND
      g.timestamp < '2019-03-03'
GROUP BY g.table_name;

In particular, this can make use of an index on table_growth(timestamp, table_name, rows).

Upvotes: 0

Nikhil
Nikhil

Reputation: 3950

this will work;

select distinct a.table_name,(a.rows-b.rows) diff from table_growth a,table_growth b 
where a.table_name=b.table_name;

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522719

Your second query is on the right track, but the join condition is partially off. You should be asserting that the table names, not ids, match:

SELECT
    x.table_name,
    (x.rows - y.rows) AS diff
FROM table_growth x
INNER JOIN table_growth y
    ON x.table_name = y.table_name and
       DATE(y.timestamp) = '2019-03-02'
WHERE
    DATE(x.timestamp) = '2019-03-01';

Note: Your current output is slightly ambiguous, because it is not clear which rows value comes first in the difference, or if perhaps you want to report an absolute value.

Upvotes: 1

Related Questions