-- Script to fix NULL user_name values in activities table -- This will populate user_name from the users table based on user_id -- First, let's see how many records have NULL user_name SELECT COUNT(*) as null_username_count FROM activities WHERE user_name IS NULL AND user_id IS NOT NULL; -- Update activities with user names from users table UPDATE activities a SET user_name = u.display_name FROM users u WHERE a.user_id = u.user_id AND a.user_name IS NULL AND u.display_name IS NOT NULL; -- For users without display_name, use email UPDATE activities a SET user_name = u.email FROM users u WHERE a.user_id = u.user_id AND a.user_name IS NULL AND u.email IS NOT NULL; -- Verify the update SELECT COUNT(*) as total_activities, COUNT(CASE WHEN user_name IS NULL THEN 1 END) as null_usernames, COUNT(CASE WHEN user_name IS NOT NULL THEN 1 END) as populated_usernames FROM activities; -- Show sample of fixed records SELECT activity_id, user_id, user_name, activity_type, activity_description, created_at FROM activities ORDER BY created_at DESC LIMIT 10;