1

I have a query that loads data from a text file into a table, finds a row with a max value in the "date_time" column and deletes all rows that are less than the max value. But this file will be updated several times a day and each time only one row with the max value will remain in the table.

LOAD DATA INFILE 'C:/users/user/desktop/download.txt' IGNORE INTO TABLE download1
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\r\n'
IGNORE 3 LINES;
DELETE FROM download1 WHERE date_time < (SELECT * FROM (SELECT MAX(date_time) AS MaxDatetime FROM download1) AS t)

How can I make the past max value also remain in the table when executing a query with an updated file?

text file:

text file

table:

table

4
  • You want to keep the record(s) which have the max(date_time) for every load? Commented Jul 7, 2020 at 8:23
  • Yes. I want to keep the records which have the max(date_time) for every load. Commented Jul 7, 2020 at 8:27
  • Does the load file keep getting added to, what does download1 table look like, what does the data file look like, are you able to amend the download1 table. Too many unknowns to provide an answer. Commented Jul 7, 2020 at 8:36
  • I edited the question. Added what the file and table look like. The file has a limit on the number of lines, that is, when updating, the data in it is overwritten. Commented Jul 7, 2020 at 9:05

1 Answer 1

1

Updated based on question edit and comments.

Since the id field in the table is auto_increment, it provides a continuously increading value. Get the max value of the id field before uploading your new file and use that to limit your delete to newer records only:

SET @OLDMAXID = IFNULL((SELECT MAX(id) FROM download1), 0);
LOAD DATA INFILE 'C:/users/user/desktop/download.txt' IGNORE INTO TABLE download1
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\r\n'
IGNORE 3 LINES;
DELETE FROM download1 WHERE date_time < (SELECT * FROM (SELECT MAX(date_time) AS MaxDatetime FROM download1) AS t) and id > @OLDMAXID;
Sign up to request clarification or add additional context in comments.

7 Comments

The query saves the row with the maximum value in the table, but when I try to execute the query with the updated file, it just inserts all the rows from the file, not just the new row with the max value.
It is supposed to insert all lines, the delete removes the ones you do not need. If the new delete does not remove the lines you want to, then the assumtion I made at the beginning of my answer may not be correct. Have you checked that?
I think your assumption is correct. Query works first time, but when file updated, it not "removes the ones you do not need" (lines that less that line with max value). It just insert all lines from file.
Then you need to check what value @OLDMAXDATE has.
The timestamp in your table is the same as your max timestamp in your csv file, this is why the max(date_time) technique does not work. I noticed that there is an id field in the table. Is that an auto increment field?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.