Assuming that you have a table called session_records
with columns id
, session_id
, and timestamp
(which stores the time the session was created), you can use the following query to remove all duplicate session records except for the most recent one:
DELETE FROM session_records
WHERE id NOT IN
(SELECT id FROM
(SELECT id, session_id, timestamp
FROM session_records
ORDER BY timestamp DESC)
AS t
GROUP BY session_id)
This query selects all session records, orders them by timestamp in descending order, and groups them by sessionid. Then it keeps only the records with the highest timestamp for each sessionid and deletes all the others.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-07-07 14:32:10 +0000
Seen: 7 times
Last updated: Jul 07 '23
Why is it that in some cases, not all records are inserted with the use of nhibernate transactions?
How can I conceal records from the datagrid that have an "active" status in the workflow?
Is it possible to have a hasMany relationship that includes null values?
How can one prevent the occurrence of duplicate records when uploading a CSV file into BigQuery?
What is the process to remove all records from an Azure table?
What is the procedure for using Sequelize's bulkCreate functionality?