ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Historical Snapshot of User Activity

SQL MariaDB

Goal -- WPM

Ready
Exercise Algorithm Area
1SELECT
2ua.user_id,
3u.username,
4ua.activity_type,
5ua.activity_timestamp
6FROM
7user_activity_history ua
8JOIN
9users u ON ua.user_id = u.user_id
10WHERE
11ua.valid_from <= '2023-01-01 00:00:00' -- Snapshot date
12AND (ua.valid_to IS NULL OR ua.valid_to > '2023-01-01 00:00:00'); -- Ensure it was active at snapshot date
13
14-- Helper function to get a user's activity at a specific historical point
15DELIMITER $$
16CREATE FUNCTION GetUserActivityAtTime(
17p_user_id INT,
18target_time DATETIME
19) RETURNS TEXT
20READS SQL DATA
21BEGIN
22DECLARE activity_log TEXT DEFAULT '';
23DECLARE current_activity_type VARCHAR(50);
24DECLARE current_activity_timestamp DATETIME;
25DECLARE done INT DEFAULT FALSE;
26
27DECLARE activity_cursor CURSOR FOR
28SELECT
29activity_type,
30activity_timestamp
31FROM
32user_activity_history
33WHERE
34user_id = p_user_id
35AND valid_from <= target_time
36AND (valid_to IS NULL OR valid_to > target_time)
37ORDER BY
38activity_timestamp ASC;
39
40DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
41
42OPEN activity_cursor;
43
44read_loop: LOOP
45FETCH activity_cursor INTO current_activity_type, current_activity_timestamp;
46IF done THEN
47LEAVE read_loop;
48END IF;
49SET activity_log = CONCAT(activity_log, 'Type: ', current_activity_type, ', Time: ', current_activity_timestamp, '\n');
50END LOOP;
51
52CLOSE activity_cursor;
53RETURN IF(activity_log = '', 'No activity found', activity_log);
54END$$
55DELIMITER ;
56
57-- Edge case: User ID does not exist.
58-- The JOIN with the 'users' table will filter out records for non-existent users.
59-- The helper function will return 'No activity found' if the user_id is not in user_activity_history.
60
61-- Edge case: No activity records for the specified date.
62-- The WHERE clause will filter out all records, resulting in an empty set for the main query.
63-- The helper function will return 'No activity found'.
64
65-- Edge case: Records with `valid_to` as NULL.
66-- These are treated as currently active records with no end date.
67-- The condition `(ua.valid_to IS NULL OR ua.valid_to > '2023-01-01 00:00:00')` handles this correctly.
68
69-- This query assumes that `user_activity_history` contains records representing states or events at different times,
70-- and that `valid_from` and `valid_to` define the period of validity for each record.
71
72-- For true historical snapshots, ensure that `valid_to` is consistently managed, often set to the `valid_from` of the next record.
Algorithm description viewbox

Historical Snapshot of User Activity

Algorithm description:

This SQL query generates a historical snapshot of user activity by selecting records from a `user_activity_history` table that were valid at a specific past date. It joins with a `users` table to include usernames. This is essential for auditing, compliance, and analyzing user behavior trends over time, allowing you to reconstruct past states of data.

Algorithm explanation:

The query selects user activity records from `user_activity_history` where the record's validity period (`valid_from` to `valid_to`) encompasses the specified snapshot date ('2023-01-01 00:00:00'). The condition `ua.valid_from <= '2023-01-01 00:00:00'` ensures the record was active on or before the snapshot date, and `(ua.valid_to IS NULL OR ua.valid_to > '2023-01-01 00:00:00')` ensures it was still active at that time (or has no end date). Time complexity is O(N log N) or O(N) depending on indexing, where N is the number of history records. Space complexity is O(1). Edge cases include non-existent users, users with no activity history, and records with `NULL` `valid_to` dates.

Pseudocode:

SELECT user_id, username, activity_type, activity_timestamp FROM user_activity_history JOIN users ON user_id WHERE valid_from <= snapshot_date AND (valid_to IS NULL OR valid_to > snapshot_date).