im trying to find the last time a value was inputted into table 1 and then input that value into table 2 however due to the sheer size of table 1 (this gets truncated daily) it either takes forever or i end up losing connection to the server
I tried to use a after insert trigger but it would run i then created an event to run every couple hours but again after the 1st time it would run.
The query im currently using is
replace into database.table2 (value1,value2,date,time)
select value1,value2,date,time from database.table1;
value 1 is a unique value which is the reason for using replace as I only want 1 instance (string) of this value.
Example Data Table1
----------------------------------------------------
| Value1 | Value2 | DATE | TIME |
--------------------------------------------------
| 1 | red | 01/08/2018 | 10:33:15 |
| 2 | red | 01/08/2018 | 10:33:20 |
| 3 | red | 01/08/2018 | 10:33:21 |
| 4 | red | 01/08/2018 | 10:33:23 |
| 1 | red | 01/08/2018 | 10:36:15 |
| 1 | red | 01/08/2018 | 10:38:15 |
| 2 | red | 01/08/2018 | 10:38:15 |
----------------------------------------------------
Example Data Table2 Desired Outcome where the value pulled over from table1 is the latest value in the table
----------------------------------------------------
| Value1 | Value2 | DATE | TIME |
--------------------------------------------------
| 1 | red | 01/08/2018 | 10:38:15 |
| 2 | red | 01/08/2018 | 10:38:15 |
| 3 | red | 01/08/2018 | 10:33:21 |
| 4 | red | 01/08/2018 | 10:33:23 |
----------------------------------------------------
CREATE TABLE table1 (
value1 int primary key,
value2 varchar(10),
Date date,
Time time
);
INSERT INTO table1 (value1,value2, Date, Time)
Values (1, 'red', '2018-08-01', '10:33:15' ),
(2, 'red', '2018-08-01', '10:33:20' );
(3, 'red', '2018-08-01', '10:33:21' );
(4, 'red', '2018-08-01', '10:33:23' );
(1, 'red', '2018-08-01', '10:36:15' );
(1, 'red', '2018-08-01', '10:18:15' );
(2, 'red', '2018-08-01', '10:38:15' );
CREATE TABLE table2 (
value1 varchar(10) primary key,
value2 varchar(10),
Date date,
Time time
);
Hopefully i have written those correctly as i normally use workbench to create tables