INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-01 00:00:00' AND '2024-03-02 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-01 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-01 00:00:00' AND '2024-03-05 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-02 00:00:00' AND '2024-03-03 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-02 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-02 00:00:00' AND '2024-03-06 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-03 00:00:00' AND '2024-03-04 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-03 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-03 00:00:00' AND '2024-03-07 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-04 00:00:00' AND '2024-03-05 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-04 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-04 00:00:00' AND '2024-03-08 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-05 00:00:00' AND '2024-03-06 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-05 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-05 00:00:00' AND '2024-03-09 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-06 00:00:00' AND '2024-03-07 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-06 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-06 00:00:00' AND '2024-03-10 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-07 00:00:00' AND '2024-03-08 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-07 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-07 00:00:00' AND '2024-03-11 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-08 00:00:00' AND '2024-03-09 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-08 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-08 00:00:00' AND '2024-03-12 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-09 00:00:00' AND '2024-03-10 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-09 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-09 00:00:00' AND '2024-03-13 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-10 00:00:00' AND '2024-03-11 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-10 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-10 00:00:00' AND '2024-03-14 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-11 00:00:00' AND '2024-03-12 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-11 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-11 00:00:00' AND '2024-03-15 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-12 00:00:00' AND '2024-03-13 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-12 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-12 00:00:00' AND '2024-03-16 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-13 00:00:00' AND '2024-03-14 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-13 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-13 00:00:00' AND '2024-03-17 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-14 00:00:00' AND '2024-03-15 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-14 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-14 00:00:00' AND '2024-03-18 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-15 00:00:00' AND '2024-03-16 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-15 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-15 00:00:00' AND '2024-03-19 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-16 00:00:00' AND '2024-03-17 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-16 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-16 00:00:00' AND '2024-03-20 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-17 00:00:00' AND '2024-03-18 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-17 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-17 00:00:00' AND '2024-03-21 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-18 00:00:00' AND '2024-03-19 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-18 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-18 00:00:00' AND '2024-03-22 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-19 00:00:00' AND '2024-03-20 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-19 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-19 00:00:00' AND '2024-03-23 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-20 00:00:00' AND '2024-03-21 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-20 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-20 00:00:00' AND '2024-03-24 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-21 00:00:00' AND '2024-03-22 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-21 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-21 00:00:00' AND '2024-03-25 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-22 00:00:00' AND '2024-03-23 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-22 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-22 00:00:00' AND '2024-03-26 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-23 00:00:00' AND '2024-03-24 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-23 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-23 00:00:00' AND '2024-03-27 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-24 00:00:00' AND '2024-03-25 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-24 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-24 00:00:00' AND '2024-03-28 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-25 00:00:00' AND '2024-03-26 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-25 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-25 00:00:00' AND '2024-03-29 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-26 00:00:00' AND '2024-03-27 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-26 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-26 00:00:00' AND '2024-03-30 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-27 00:00:00' AND '2024-03-28 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-27 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-27 00:00:00' AND '2024-03-31 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-28 00:00:00' AND '2024-03-29 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-28 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-28 00:00:00' AND '2024-04-01 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-29 00:00:00' AND '2024-03-30 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-29 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-29 00:00:00' AND '2024-04-02 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-30 00:00:00' AND '2024-03-31 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-30 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-30 00:00:00' AND '2024-04-03 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-03-31 00:00:00' AND '2024-04-01 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-03-31 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-03-31 00:00:00' AND '2024-04-04 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-01 00:00:00' AND '2024-04-02 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-01 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-01 00:00:00' AND '2024-04-05 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-02 00:00:00' AND '2024-04-03 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-02 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-02 00:00:00' AND '2024-04-06 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-03 00:00:00' AND '2024-04-04 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-03 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-03 00:00:00' AND '2024-04-07 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-04 00:00:00' AND '2024-04-05 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-04 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-04 00:00:00' AND '2024-04-08 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-05 00:00:00' AND '2024-04-06 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-05 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-05 00:00:00' AND '2024-04-09 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-06 00:00:00' AND '2024-04-07 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-06 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-06 00:00:00' AND '2024-04-10 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-07 00:00:00' AND '2024-04-08 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-07 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-07 00:00:00' AND '2024-04-11 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-08 00:00:00' AND '2024-04-09 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-08 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-08 00:00:00' AND '2024-04-12 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-09 00:00:00' AND '2024-04-10 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-09 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-09 00:00:00' AND '2024-04-13 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-10 00:00:00' AND '2024-04-11 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-10 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-10 00:00:00' AND '2024-04-14 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-11 00:00:00' AND '2024-04-12 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-11 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-11 00:00:00' AND '2024-04-15 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-12 00:00:00' AND '2024-04-13 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-12 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-12 00:00:00' AND '2024-04-16 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-13 00:00:00' AND '2024-04-14 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-13 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-13 00:00:00' AND '2024-04-17 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-14 00:00:00' AND '2024-04-15 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-14 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-14 00:00:00' AND '2024-04-18 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-15 00:00:00' AND '2024-04-16 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-15 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-15 00:00:00' AND '2024-04-19 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-16 00:00:00' AND '2024-04-17 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-16 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-16 00:00:00' AND '2024-04-20 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-17 00:00:00' AND '2024-04-18 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-17 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-17 00:00:00' AND '2024-04-21 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-18 00:00:00' AND '2024-04-19 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-18 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-18 00:00:00' AND '2024-04-22 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-19 00:00:00' AND '2024-04-20 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-19 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-19 00:00:00' AND '2024-04-23 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-20 00:00:00' AND '2024-04-21 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-20 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-20 00:00:00' AND '2024-04-24 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-21 00:00:00' AND '2024-04-22 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-21 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-21 00:00:00' AND '2024-04-25 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-22 00:00:00' AND '2024-04-23 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-22 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-22 00:00:00' AND '2024-04-26 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-23 00:00:00' AND '2024-04-24 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-23 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-23 00:00:00' AND '2024-04-27 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-24 00:00:00' AND '2024-04-25 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-24 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-24 00:00:00' AND '2024-04-28 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-25 00:00:00' AND '2024-04-26 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-25 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-25 00:00:00' AND '2024-04-29 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-26 00:00:00' AND '2024-04-27 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-26 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-26 00:00:00' AND '2024-04-30 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-27 00:00:00' AND '2024-04-28 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-27 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-27 00:00:00' AND '2024-05-01 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-28 00:00:00' AND '2024-04-29 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-28 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-28 00:00:00' AND '2024-05-02 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-29 00:00:00' AND '2024-04-30 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-29 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-29 00:00:00' AND '2024-05-03 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-04-30 00:00:00' AND '2024-05-01 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-04-30 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-04-30 00:00:00' AND '2024-05-04 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-01 00:00:00' AND '2024-05-02 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-01 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-01 00:00:00' AND '2024-05-05 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-02 00:00:00' AND '2024-05-03 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-02 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-02 00:00:00' AND '2024-05-06 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-03 00:00:00' AND '2024-05-04 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-03 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-03 00:00:00' AND '2024-05-07 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-04 00:00:00' AND '2024-05-05 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-04 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-04 00:00:00' AND '2024-05-08 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-05 00:00:00' AND '2024-05-06 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-05 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-05 00:00:00' AND '2024-05-09 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-06 00:00:00' AND '2024-05-07 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-06 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-06 00:00:00' AND '2024-05-10 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-07 00:00:00' AND '2024-05-08 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-07 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-07 00:00:00' AND '2024-05-11 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-08 00:00:00' AND '2024-05-09 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-08 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-08 00:00:00' AND '2024-05-12 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-09 00:00:00' AND '2024-05-10 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-09 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-09 00:00:00' AND '2024-05-13 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-10 00:00:00' AND '2024-05-11 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-10 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-10 00:00:00' AND '2024-05-14 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-11 00:00:00' AND '2024-05-12 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-11 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-11 00:00:00' AND '2024-05-15 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-12 00:00:00' AND '2024-05-13 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-12 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-12 00:00:00' AND '2024-05-16 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-13 00:00:00' AND '2024-05-14 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-13 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-13 00:00:00' AND '2024-05-17 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-14 00:00:00' AND '2024-05-15 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-14 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-14 00:00:00' AND '2024-05-18 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-15 00:00:00' AND '2024-05-16 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-15 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-15 00:00:00' AND '2024-05-19 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-16 00:00:00' AND '2024-05-17 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-16 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-16 00:00:00' AND '2024-05-20 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-17 00:00:00' AND '2024-05-18 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-17 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-17 00:00:00' AND '2024-05-21 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-18 00:00:00' AND '2024-05-19 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-18 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-18 00:00:00' AND '2024-05-22 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-19 00:00:00' AND '2024-05-20 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-19 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-19 00:00:00' AND '2024-05-23 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-20 00:00:00' AND '2024-05-21 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-20 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-20 00:00:00' AND '2024-05-24 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-21 00:00:00' AND '2024-05-22 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-21 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-21 00:00:00' AND '2024-05-25 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-22 00:00:00' AND '2024-05-23 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-22 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-22 00:00:00' AND '2024-05-26 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-23 00:00:00' AND '2024-05-24 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-23 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-23 00:00:00' AND '2024-05-27 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-24 00:00:00' AND '2024-05-25 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-24 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-24 00:00:00' AND '2024-05-28 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-25 00:00:00' AND '2024-05-26 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-25 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-25 00:00:00' AND '2024-05-29 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-26 00:00:00' AND '2024-05-27 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-26 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-26 00:00:00' AND '2024-05-30 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-27 00:00:00' AND '2024-05-28 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-27 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-27 00:00:00' AND '2024-05-31 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-28 00:00:00' AND '2024-05-29 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-28 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-28 00:00:00' AND '2024-06-01 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-29 00:00:00' AND '2024-05-30 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-29 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-29 00:00:00' AND '2024-06-02 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-30 00:00:00' AND '2024-05-31 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-30 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-30 00:00:00' AND '2024-06-03 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-05-31 00:00:00' AND '2024-06-01 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-05-31 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-05-31 00:00:00' AND '2024-06-04 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-01 00:00:00' AND '2024-06-02 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-01 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-01 00:00:00' AND '2024-06-05 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-02 00:00:00' AND '2024-06-03 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-02 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-02 00:00:00' AND '2024-06-06 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-03 00:00:00' AND '2024-06-04 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-03 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-03 00:00:00' AND '2024-06-07 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-04 00:00:00' AND '2024-06-05 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-04 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-04 00:00:00' AND '2024-06-08 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-05 00:00:00' AND '2024-06-06 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-05 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-05 00:00:00' AND '2024-06-09 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-06 00:00:00' AND '2024-06-07 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-06 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-06 00:00:00' AND '2024-06-10 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-07 00:00:00' AND '2024-06-08 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-07 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-07 00:00:00' AND '2024-06-11 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-08 00:00:00' AND '2024-06-09 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-08 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-08 00:00:00' AND '2024-06-12 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-09 00:00:00' AND '2024-06-10 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-09 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-09 00:00:00' AND '2024-06-13 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-10 00:00:00' AND '2024-06-11 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-10 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-10 00:00:00' AND '2024-06-14 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-11 00:00:00' AND '2024-06-12 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-11 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-11 00:00:00' AND '2024-06-15 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-12 00:00:00' AND '2024-06-13 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-12 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-12 00:00:00' AND '2024-06-16 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-13 00:00:00' AND '2024-06-14 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-13 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-13 00:00:00' AND '2024-06-17 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-14 00:00:00' AND '2024-06-15 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-14 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-14 00:00:00' AND '2024-06-18 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-15 00:00:00' AND '2024-06-16 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-15 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-15 00:00:00' AND '2024-06-19 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-16 00:00:00' AND '2024-06-17 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-16 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-16 00:00:00' AND '2024-06-20 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-17 00:00:00' AND '2024-06-18 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-17 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-17 00:00:00' AND '2024-06-21 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-18 00:00:00' AND '2024-06-19 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-18 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-18 00:00:00' AND '2024-06-22 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-19 00:00:00' AND '2024-06-20 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-19 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-19 00:00:00' AND '2024-06-23 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-20 00:00:00' AND '2024-06-21 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-20 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-20 00:00:00' AND '2024-06-24 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-21 00:00:00' AND '2024-06-22 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-21 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-21 00:00:00' AND '2024-06-25 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-22 00:00:00' AND '2024-06-23 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-22 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-22 00:00:00' AND '2024-06-26 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-23 00:00:00' AND '2024-06-24 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-23 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-23 00:00:00' AND '2024-06-27 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-24 00:00:00' AND '2024-06-25 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-24 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-24 00:00:00' AND '2024-06-28 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-25 00:00:00' AND '2024-06-26 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-25 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-25 00:00:00' AND '2024-06-29 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-26 00:00:00' AND '2024-06-27 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-26 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-26 00:00:00' AND '2024-06-30 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-27 00:00:00' AND '2024-06-28 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-27 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-27 00:00:00' AND '2024-07-01 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-28 00:00:00' AND '2024-06-29 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-28 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-28 00:00:00' AND '2024-07-02 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-29 00:00:00' AND '2024-06-30 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-29 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-29 00:00:00' AND '2024-07-03 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-06-30 00:00:00' AND '2024-07-01 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-06-30 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-06-30 00:00:00' AND '2024-07-04 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-01 00:00:00' AND '2024-07-02 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-01 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-01 00:00:00' AND '2024-07-05 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-02 00:00:00' AND '2024-07-03 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-02 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-02 00:00:00' AND '2024-07-06 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-03 00:00:00' AND '2024-07-04 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-03 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-03 00:00:00' AND '2024-07-07 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-04 00:00:00' AND '2024-07-05 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-04 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-04 00:00:00' AND '2024-07-08 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-05 00:00:00' AND '2024-07-06 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-05 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-05 00:00:00' AND '2024-07-09 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-06 00:00:00' AND '2024-07-07 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-06 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-06 00:00:00' AND '2024-07-10 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-07 00:00:00' AND '2024-07-08 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-07 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-07 00:00:00' AND '2024-07-11 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-08 00:00:00' AND '2024-07-09 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-08 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-08 00:00:00' AND '2024-07-12 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-09 00:00:00' AND '2024-07-10 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-09 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-09 00:00:00' AND '2024-07-13 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-10 00:00:00' AND '2024-07-11 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-10 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-10 00:00:00' AND '2024-07-14 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-11 00:00:00' AND '2024-07-12 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-11 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-11 00:00:00' AND '2024-07-15 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-12 00:00:00' AND '2024-07-13 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-12 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-12 00:00:00' AND '2024-07-16 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-13 00:00:00' AND '2024-07-14 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-13 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-13 00:00:00' AND '2024-07-17 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-14 00:00:00' AND '2024-07-15 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-14 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-14 00:00:00' AND '2024-07-18 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-15 00:00:00' AND '2024-07-16 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-15 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-15 00:00:00' AND '2024-07-19 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-16 00:00:00' AND '2024-07-17 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-16 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-16 00:00:00' AND '2024-07-20 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-17 00:00:00' AND '2024-07-18 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-17 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-17 00:00:00' AND '2024-07-21 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-18 00:00:00' AND '2024-07-19 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-18 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-18 00:00:00' AND '2024-07-22 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-19 00:00:00' AND '2024-07-20 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-19 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-19 00:00:00' AND '2024-07-23 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-20 00:00:00' AND '2024-07-21 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-20 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-20 00:00:00' AND '2024-07-24 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-21 00:00:00' AND '2024-07-22 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-21 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-21 00:00:00' AND '2024-07-25 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-22 00:00:00' AND '2024-07-23 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-22 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-22 00:00:00' AND '2024-07-26 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-23 00:00:00' AND '2024-07-24 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-23 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-23 00:00:00' AND '2024-07-27 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-24 00:00:00' AND '2024-07-25 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-24 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-24 00:00:00' AND '2024-07-28 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-25 00:00:00' AND '2024-07-26 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-25 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-25 00:00:00' AND '2024-07-29 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-26 00:00:00' AND '2024-07-27 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-26 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-26 00:00:00' AND '2024-07-30 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-27 00:00:00' AND '2024-07-28 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-27 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-27 00:00:00' AND '2024-07-31 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-28 00:00:00' AND '2024-07-29 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-28 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-28 00:00:00' AND '2024-08-01 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-29 00:00:00' AND '2024-07-30 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-29 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-29 00:00:00' AND '2024-08-02 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-30 00:00:00' AND '2024-07-31 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-30 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-30 00:00:00' AND '2024-08-03 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-07-31 00:00:00' AND '2024-08-01 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-07-31 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-07-31 00:00:00' AND '2024-08-04 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-01 00:00:00' AND '2024-08-02 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-01 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-01 00:00:00' AND '2024-08-05 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-02 00:00:00' AND '2024-08-03 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-02 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-02 00:00:00' AND '2024-08-06 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-03 00:00:00' AND '2024-08-04 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-03 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-03 00:00:00' AND '2024-08-07 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-04 00:00:00' AND '2024-08-05 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-04 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-04 00:00:00' AND '2024-08-08 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-05 00:00:00' AND '2024-08-06 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-05 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-05 00:00:00' AND '2024-08-09 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-06 00:00:00' AND '2024-08-07 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-06 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-06 00:00:00' AND '2024-08-10 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-07 00:00:00' AND '2024-08-08 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-07 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-07 00:00:00' AND '2024-08-11 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-08 00:00:00' AND '2024-08-09 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-08 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-08 00:00:00' AND '2024-08-12 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-09 00:00:00' AND '2024-08-10 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-09 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-09 00:00:00' AND '2024-08-13 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-10 00:00:00' AND '2024-08-11 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-10 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-10 00:00:00' AND '2024-08-14 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-11 00:00:00' AND '2024-08-12 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-11 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-11 00:00:00' AND '2024-08-15 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-12 00:00:00' AND '2024-08-13 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-12 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-12 00:00:00' AND '2024-08-16 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-13 00:00:00' AND '2024-08-14 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-13 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-13 00:00:00' AND '2024-08-17 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-14 00:00:00' AND '2024-08-15 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-14 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-14 00:00:00' AND '2024-08-18 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-15 00:00:00' AND '2024-08-16 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-15 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-15 00:00:00' AND '2024-08-19 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-16 00:00:00' AND '2024-08-17 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-16 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-16 00:00:00' AND '2024-08-20 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-17 00:00:00' AND '2024-08-18 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-17 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-17 00:00:00' AND '2024-08-21 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-18 00:00:00' AND '2024-08-19 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-18 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-18 00:00:00' AND '2024-08-22 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-19 00:00:00' AND '2024-08-20 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-19 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-19 00:00:00' AND '2024-08-23 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-20 00:00:00' AND '2024-08-21 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-20 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-20 00:00:00' AND '2024-08-24 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-21 00:00:00' AND '2024-08-22 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-21 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-21 00:00:00' AND '2024-08-25 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-22 00:00:00' AND '2024-08-23 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-22 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-22 00:00:00' AND '2024-08-26 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-23 00:00:00' AND '2024-08-24 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-23 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-23 00:00:00' AND '2024-08-27 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-24 00:00:00' AND '2024-08-25 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-24 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-24 00:00:00' AND '2024-08-28 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-25 00:00:00' AND '2024-08-26 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-25 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-25 00:00:00' AND '2024-08-29 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-26 00:00:00' AND '2024-08-27 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-26 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-26 00:00:00' AND '2024-08-30 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-27 00:00:00' AND '2024-08-28 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-27 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-27 00:00:00' AND '2024-08-31 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-28 00:00:00' AND '2024-08-29 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-28 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-28 00:00:00' AND '2024-09-01 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-29 00:00:00' AND '2024-08-30 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-29 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-29 00:00:00' AND '2024-09-02 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-30 00:00:00' AND '2024-08-31 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-30 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-30 00:00:00' AND '2024-09-03 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-08-31 00:00:00' AND '2024-09-01 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-08-31 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-08-31 00:00:00' AND '2024-09-04 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-01 00:00:00' AND '2024-09-02 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-01 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-01 00:00:00' AND '2024-09-05 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-02 00:00:00' AND '2024-09-03 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-02 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-02 00:00:00' AND '2024-09-06 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-03 00:00:00' AND '2024-09-04 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-03 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-03 00:00:00' AND '2024-09-07 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-04 00:00:00' AND '2024-09-05 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-04 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-04 00:00:00' AND '2024-09-08 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-05 00:00:00' AND '2024-09-06 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-05 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-05 00:00:00' AND '2024-09-09 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-06 00:00:00' AND '2024-09-07 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-06 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-06 00:00:00' AND '2024-09-10 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-07 00:00:00' AND '2024-09-08 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-07 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-07 00:00:00' AND '2024-09-11 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-08 00:00:00' AND '2024-09-09 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-08 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-08 00:00:00' AND '2024-09-12 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-09 00:00:00' AND '2024-09-10 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-09 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-09 00:00:00' AND '2024-09-13 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-10 00:00:00' AND '2024-09-11 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-10 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-10 00:00:00' AND '2024-09-14 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-11 00:00:00' AND '2024-09-12 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-11 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-11 00:00:00' AND '2024-09-15 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-12 00:00:00' AND '2024-09-13 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-12 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-12 00:00:00' AND '2024-09-16 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-13 00:00:00' AND '2024-09-14 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-13 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-13 00:00:00' AND '2024-09-17 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-14 00:00:00' AND '2024-09-15 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-14 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-14 00:00:00' AND '2024-09-18 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-15 00:00:00' AND '2024-09-16 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-15 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-15 00:00:00' AND '2024-09-19 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-16 00:00:00' AND '2024-09-17 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-16 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-16 00:00:00' AND '2024-09-20 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-17 00:00:00' AND '2024-09-18 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-17 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-17 00:00:00' AND '2024-09-21 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-18 00:00:00' AND '2024-09-19 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-18 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-18 00:00:00' AND '2024-09-22 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-19 00:00:00' AND '2024-09-20 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-19 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-19 00:00:00' AND '2024-09-23 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-20 00:00:00' AND '2024-09-21 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-20 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-20 00:00:00' AND '2024-09-24 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-21 00:00:00' AND '2024-09-22 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-21 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-21 00:00:00' AND '2024-09-25 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-22 00:00:00' AND '2024-09-23 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-22 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-22 00:00:00' AND '2024-09-26 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-23 00:00:00' AND '2024-09-24 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-23 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-23 00:00:00' AND '2024-09-27 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-24 00:00:00' AND '2024-09-25 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-24 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-24 00:00:00' AND '2024-09-28 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-25 00:00:00' AND '2024-09-26 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-25 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-25 00:00:00' AND '2024-09-29 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-26 00:00:00' AND '2024-09-27 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-26 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-26 00:00:00' AND '2024-09-30 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-27 00:00:00' AND '2024-09-28 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-27 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-27 00:00:00' AND '2024-10-01 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-28 00:00:00' AND '2024-09-29 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-28 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-28 00:00:00' AND '2024-10-02 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-29 00:00:00' AND '2024-09-30 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-29 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-29 00:00:00' AND '2024-10-03 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-09-30 00:00:00' AND '2024-10-01 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-09-30 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-09-30 00:00:00' AND '2024-10-04 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-01 00:00:00' AND '2024-10-02 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-01 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-01 00:00:00' AND '2024-10-05 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-02 00:00:00' AND '2024-10-03 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-02 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-02 00:00:00' AND '2024-10-06 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-03 00:00:00' AND '2024-10-04 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-03 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-03 00:00:00' AND '2024-10-07 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-04 00:00:00' AND '2024-10-05 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-04 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-04 00:00:00' AND '2024-10-08 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-05 00:00:00' AND '2024-10-06 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-05 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-05 00:00:00' AND '2024-10-09 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-06 00:00:00' AND '2024-10-07 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-06 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-06 00:00:00' AND '2024-10-10 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-07 00:00:00' AND '2024-10-08 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-07 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-07 00:00:00' AND '2024-10-11 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-08 00:00:00' AND '2024-10-09 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-08 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-08 00:00:00' AND '2024-10-12 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-09 00:00:00' AND '2024-10-10 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-09 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-09 00:00:00' AND '2024-10-13 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-10 00:00:00' AND '2024-10-11 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-10 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-10 00:00:00' AND '2024-10-14 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-11 00:00:00' AND '2024-10-12 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-11 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-11 00:00:00' AND '2024-10-15 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-12 00:00:00' AND '2024-10-13 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-12 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-12 00:00:00' AND '2024-10-16 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-13 00:00:00' AND '2024-10-14 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-13 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-13 00:00:00' AND '2024-10-17 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-14 00:00:00' AND '2024-10-15 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-14 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-14 00:00:00' AND '2024-10-18 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-15 00:00:00' AND '2024-10-16 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-15 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-15 00:00:00' AND '2024-10-19 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-16 00:00:00' AND '2024-10-17 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-16 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-16 00:00:00' AND '2024-10-20 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-17 00:00:00' AND '2024-10-18 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-17 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-17 00:00:00' AND '2024-10-21 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-18 00:00:00' AND '2024-10-19 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-18 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-18 00:00:00' AND '2024-10-22 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-19 00:00:00' AND '2024-10-20 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-19 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-19 00:00:00' AND '2024-10-23 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-20 00:00:00' AND '2024-10-21 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-20 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-20 00:00:00' AND '2024-10-24 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-21 00:00:00' AND '2024-10-22 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-21 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-21 00:00:00' AND '2024-10-25 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-22 00:00:00' AND '2024-10-23 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-22 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-22 00:00:00' AND '2024-10-26 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-23 00:00:00' AND '2024-10-24 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-23 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-23 00:00:00' AND '2024-10-27 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-24 00:00:00' AND '2024-10-25 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-24 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-24 00:00:00' AND '2024-10-28 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-25 00:00:00' AND '2024-10-26 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-25 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-25 00:00:00' AND '2024-10-29 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-26 00:00:00' AND '2024-10-27 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-26 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-26 00:00:00' AND '2024-10-30 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-27 00:00:00' AND '2024-10-28 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-27 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-27 00:00:00' AND '2024-10-31 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-28 00:00:00' AND '2024-10-29 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-28 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-28 00:00:00' AND '2024-11-01 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-29 00:00:00' AND '2024-10-30 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-29 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-29 00:00:00' AND '2024-11-02 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-30 00:00:00' AND '2024-10-31 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-30 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-30 00:00:00' AND '2024-11-03 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-10-31 00:00:00' AND '2024-11-01 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-10-31 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-10-31 00:00:00' AND '2024-11-04 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-01 00:00:00' AND '2024-11-02 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-01 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-01 00:00:00' AND '2024-11-05 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-02 00:00:00' AND '2024-11-03 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-02 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-02 00:00:00' AND '2024-11-06 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-03 00:00:00' AND '2024-11-04 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-03 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-03 00:00:00' AND '2024-11-07 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-04 00:00:00' AND '2024-11-05 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-04 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-04 00:00:00' AND '2024-11-08 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-05 00:00:00' AND '2024-11-06 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-05 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-05 00:00:00' AND '2024-11-09 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-06 00:00:00' AND '2024-11-07 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-06 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-06 00:00:00' AND '2024-11-10 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-07 00:00:00' AND '2024-11-08 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-07 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-07 00:00:00' AND '2024-11-11 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-08 00:00:00' AND '2024-11-09 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-08 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-08 00:00:00' AND '2024-11-12 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-09 00:00:00' AND '2024-11-10 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-09 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-09 00:00:00' AND '2024-11-13 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-10 00:00:00' AND '2024-11-11 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-10 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-10 00:00:00' AND '2024-11-14 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-11 00:00:00' AND '2024-11-12 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-11 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-11 00:00:00' AND '2024-11-15 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-12 00:00:00' AND '2024-11-13 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-12 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-12 00:00:00' AND '2024-11-16 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-13 00:00:00' AND '2024-11-14 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-13 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-13 00:00:00' AND '2024-11-17 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-14 00:00:00' AND '2024-11-15 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-14 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-14 00:00:00' AND '2024-11-18 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-15 00:00:00' AND '2024-11-16 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-15 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-15 00:00:00' AND '2024-11-19 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-16 00:00:00' AND '2024-11-17 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-16 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-16 00:00:00' AND '2024-11-20 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-17 00:00:00' AND '2024-11-18 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-17 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-17 00:00:00' AND '2024-11-21 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-18 00:00:00' AND '2024-11-19 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-18 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-18 00:00:00' AND '2024-11-22 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-19 00:00:00' AND '2024-11-20 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-19 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-19 00:00:00' AND '2024-11-23 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-20 00:00:00' AND '2024-11-21 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-20 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-20 00:00:00' AND '2024-11-24 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-21 00:00:00' AND '2024-11-22 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-21 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-21 00:00:00' AND '2024-11-25 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-22 00:00:00' AND '2024-11-23 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-22 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-22 00:00:00' AND '2024-11-26 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-23 00:00:00' AND '2024-11-24 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-23 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-23 00:00:00' AND '2024-11-27 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-24 00:00:00' AND '2024-11-25 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-24 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-24 00:00:00' AND '2024-11-28 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-25 00:00:00' AND '2024-11-26 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-25 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-25 00:00:00' AND '2024-11-29 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-26 00:00:00' AND '2024-11-27 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-26 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-26 00:00:00' AND '2024-11-30 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-27 00:00:00' AND '2024-11-28 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-27 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-27 00:00:00' AND '2024-12-01 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-28 00:00:00' AND '2024-11-29 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-28 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-28 00:00:00' AND '2024-12-02 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-29 00:00:00' AND '2024-11-30 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-29 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-29 00:00:00' AND '2024-12-03 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-11-30 00:00:00' AND '2024-12-01 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-11-30 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-11-30 00:00:00' AND '2024-12-04 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-01 00:00:00' AND '2024-12-02 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-01 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-01 00:00:00' AND '2024-12-05 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-02 00:00:00' AND '2024-12-03 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-02 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-02 00:00:00' AND '2024-12-06 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-03 00:00:00' AND '2024-12-04 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-03 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-03 00:00:00' AND '2024-12-07 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-04 00:00:00' AND '2024-12-05 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-04 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-04 00:00:00' AND '2024-12-08 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-05 00:00:00' AND '2024-12-06 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-05 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-05 00:00:00' AND '2024-12-09 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-06 00:00:00' AND '2024-12-07 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-06 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-06 00:00:00' AND '2024-12-10 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-07 00:00:00' AND '2024-12-08 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-07 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-07 00:00:00' AND '2024-12-11 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-08 00:00:00' AND '2024-12-09 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-08 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-08 00:00:00' AND '2024-12-12 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-09 00:00:00' AND '2024-12-10 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-09 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-09 00:00:00' AND '2024-12-13 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-10 00:00:00' AND '2024-12-11 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-10 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-10 00:00:00' AND '2024-12-14 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-11 00:00:00' AND '2024-12-12 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-11 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-11 00:00:00' AND '2024-12-15 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-12 00:00:00' AND '2024-12-13 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-12 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-12 00:00:00' AND '2024-12-16 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-13 00:00:00' AND '2024-12-14 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-13 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-13 00:00:00' AND '2024-12-17 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-14 00:00:00' AND '2024-12-15 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-14 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-14 00:00:00' AND '2024-12-18 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-15 00:00:00' AND '2024-12-16 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-15 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-15 00:00:00' AND '2024-12-19 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-16 00:00:00' AND '2024-12-17 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-16 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-16 00:00:00' AND '2024-12-20 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-17 00:00:00' AND '2024-12-18 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-17 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-17 00:00:00' AND '2024-12-21 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-18 00:00:00' AND '2024-12-19 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-18 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-18 00:00:00' AND '2024-12-22 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-19 00:00:00' AND '2024-12-20 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-19 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-19 00:00:00' AND '2024-12-23 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-20 00:00:00' AND '2024-12-21 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-20 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-20 00:00:00' AND '2024-12-24 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-21 00:00:00' AND '2024-12-22 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-21 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-21 00:00:00' AND '2024-12-25 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-22 00:00:00' AND '2024-12-23 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-22 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-22 00:00:00' AND '2024-12-26 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-23 00:00:00' AND '2024-12-24 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-23 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-23 00:00:00' AND '2024-12-27 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-24 00:00:00' AND '2024-12-25 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-24 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-24 00:00:00' AND '2024-12-28 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-25 00:00:00' AND '2024-12-26 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-25 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-25 00:00:00' AND '2024-12-29 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-26 00:00:00' AND '2024-12-27 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-26 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-26 00:00:00' AND '2024-12-30 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-27 00:00:00' AND '2024-12-28 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-27 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-27 00:00:00' AND '2024-12-31 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-28 00:00:00' AND '2024-12-29 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-28 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-28 00:00:00' AND '2025-01-01 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-29 00:00:00' AND '2024-12-30 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-29 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-29 00:00:00' AND '2025-01-02 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-30 00:00:00' AND '2024-12-31 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-30 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-30 00:00:00' AND '2025-01-03 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2024-12-31 00:00:00' AND '2025-01-01 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2024-12-31 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2024-12-31 00:00:00' AND '2025-01-04 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-01 00:00:00' AND '2025-01-02 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-01 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-01 00:00:00' AND '2025-01-05 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-02 00:00:00' AND '2025-01-03 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-02 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-02 00:00:00' AND '2025-01-06 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-03 00:00:00' AND '2025-01-04 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-03 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-03 00:00:00' AND '2025-01-07 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-04 00:00:00' AND '2025-01-05 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-04 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-04 00:00:00' AND '2025-01-08 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-05 00:00:00' AND '2025-01-06 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-05 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-05 00:00:00' AND '2025-01-09 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-06 00:00:00' AND '2025-01-07 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-06 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-06 00:00:00' AND '2025-01-10 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-07 00:00:00' AND '2025-01-08 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-07 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-07 00:00:00' AND '2025-01-11 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-08 00:00:00' AND '2025-01-09 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-08 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-08 00:00:00' AND '2025-01-12 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-09 00:00:00' AND '2025-01-10 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-09 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-09 00:00:00' AND '2025-01-13 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-10 00:00:00' AND '2025-01-11 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-10 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-10 00:00:00' AND '2025-01-14 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-11 00:00:00' AND '2025-01-12 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-11 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-11 00:00:00' AND '2025-01-15 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-12 00:00:00' AND '2025-01-13 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-12 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-12 00:00:00' AND '2025-01-16 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-13 00:00:00' AND '2025-01-14 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-13 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-13 00:00:00' AND '2025-01-17 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-14 00:00:00' AND '2025-01-15 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-14 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-14 00:00:00' AND '2025-01-18 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-15 00:00:00' AND '2025-01-16 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-15 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-15 00:00:00' AND '2025-01-19 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-16 00:00:00' AND '2025-01-17 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-16 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-16 00:00:00' AND '2025-01-20 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-17 00:00:00' AND '2025-01-18 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-17 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-17 00:00:00' AND '2025-01-21 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-18 00:00:00' AND '2025-01-19 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-18 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-18 00:00:00' AND '2025-01-22 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-19 00:00:00' AND '2025-01-20 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-19 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-19 00:00:00' AND '2025-01-23 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-20 00:00:00' AND '2025-01-21 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-20 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-20 00:00:00' AND '2025-01-24 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-21 00:00:00' AND '2025-01-22 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-21 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-21 00:00:00' AND '2025-01-25 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-22 00:00:00' AND '2025-01-23 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-22 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-22 00:00:00' AND '2025-01-26 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-23 00:00:00' AND '2025-01-24 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-23 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-23 00:00:00' AND '2025-01-27 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-24 00:00:00' AND '2025-01-25 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-24 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-24 00:00:00' AND '2025-01-28 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-25 00:00:00' AND '2025-01-26 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-25 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-25 00:00:00' AND '2025-01-29 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-26 00:00:00' AND '2025-01-27 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-26 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-26 00:00:00' AND '2025-01-30 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-27 00:00:00' AND '2025-01-28 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-27 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-27 00:00:00' AND '2025-01-31 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-28 00:00:00' AND '2025-01-29 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-28 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-28 00:00:00' AND '2025-02-01 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-29 00:00:00' AND '2025-01-30 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-29 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-29 00:00:00' AND '2025-02-02 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-30 00:00:00' AND '2025-01-31 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-30 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-30 00:00:00' AND '2025-02-03 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-01-31 00:00:00' AND '2025-02-01 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-01-31 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-01-31 00:00:00' AND '2025-02-04 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-01 00:00:00' AND '2025-02-02 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-01 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-01 00:00:00' AND '2025-02-05 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-02 00:00:00' AND '2025-02-03 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-02 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-02 00:00:00' AND '2025-02-06 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-03 00:00:00' AND '2025-02-04 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-03 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-03 00:00:00' AND '2025-02-07 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-04 00:00:00' AND '2025-02-05 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-04 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-04 00:00:00' AND '2025-02-08 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-05 00:00:00' AND '2025-02-06 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-05 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-05 00:00:00' AND '2025-02-09 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-06 00:00:00' AND '2025-02-07 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-06 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-06 00:00:00' AND '2025-02-10 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-07 00:00:00' AND '2025-02-08 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-07 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-07 00:00:00' AND '2025-02-11 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-08 00:00:00' AND '2025-02-09 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-08 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-08 00:00:00' AND '2025-02-12 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-09 00:00:00' AND '2025-02-10 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-09 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-09 00:00:00' AND '2025-02-13 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-10 00:00:00' AND '2025-02-11 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-10 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-10 00:00:00' AND '2025-02-14 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-11 00:00:00' AND '2025-02-12 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-11 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-11 00:00:00' AND '2025-02-15 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-12 00:00:00' AND '2025-02-13 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-12 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-12 00:00:00' AND '2025-02-16 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-13 00:00:00' AND '2025-02-14 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-13 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-13 00:00:00' AND '2025-02-17 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-14 00:00:00' AND '2025-02-15 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-14 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-14 00:00:00' AND '2025-02-18 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-15 00:00:00' AND '2025-02-16 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-15 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-15 00:00:00' AND '2025-02-19 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-16 00:00:00' AND '2025-02-17 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-16 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-16 00:00:00' AND '2025-02-20 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-17 00:00:00' AND '2025-02-18 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-17 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-17 00:00:00' AND '2025-02-21 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-18 00:00:00' AND '2025-02-19 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-18 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-18 00:00:00' AND '2025-02-22 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-19 00:00:00' AND '2025-02-20 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-19 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-19 00:00:00' AND '2025-02-23 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-20 00:00:00' AND '2025-02-21 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-20 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-20 00:00:00' AND '2025-02-24 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-21 00:00:00' AND '2025-02-22 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-21 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-21 00:00:00' AND '2025-02-25 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-22 00:00:00' AND '2025-02-23 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-22 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-22 00:00:00' AND '2025-02-26 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-23 00:00:00' AND '2025-02-24 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-23 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-23 00:00:00' AND '2025-02-27 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-24 00:00:00' AND '2025-02-25 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-24 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-24 00:00:00' AND '2025-02-28 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-25 00:00:00' AND '2025-02-26 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-25 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-25 00:00:00' AND '2025-03-01 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-26 00:00:00' AND '2025-02-27 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-26 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-26 00:00:00' AND '2025-03-02 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-27 00:00:00' AND '2025-02-28 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-27 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-27 00:00:00' AND '2025-03-03 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-02-28 00:00:00' AND '2025-03-01 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-02-28 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-02-28 00:00:00' AND '2025-03-04 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-03-01 00:00:00' AND '2025-03-02 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-03-01 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-03-01 00:00:00' AND '2025-03-05 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-03-02 00:00:00' AND '2025-03-03 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-03-02 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-03-02 00:00:00' AND '2025-03-06 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-03-03 00:00:00' AND '2025-03-04 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-03-03 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-03-03 00:00:00' AND '2025-03-07 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-03-04 00:00:00' AND '2025-03-05 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-03-04 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-03-04 00:00:00' AND '2025-03-08 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-03-05 00:00:00' AND '2025-03-06 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-03-05 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-03-05 00:00:00' AND '2025-03-09 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-03-06 00:00:00' AND '2025-03-07 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-03-06 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-03-06 00:00:00' AND '2025-03-10 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-03-07 00:00:00' AND '2025-03-08 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-03-07 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-03-07 00:00:00' AND '2025-03-11 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-03-08 00:00:00' AND '2025-03-09 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-03-08 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-03-08 00:00:00' AND '2025-03-12 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-03-09 00:00:00' AND '2025-03-10 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-03-09 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-03-09 00:00:00' AND '2025-03-13 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-03-10 00:00:00' AND '2025-03-11 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-03-10 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-03-10 00:00:00' AND '2025-03-14 00:00:00'
        GROUP BY e.session_id;

---

INSERT INTO openpanel.sessions
        WITH unique_sessions AS (
          SELECT session_id, min(created_at) as first_event_at
          FROM openpanel.events
          WHERE 
            created_at BETWEEN '2025-03-11 00:00:00' AND '2025-03-12 00:00:00'
            AND session_id != ''
          GROUP BY session_id
          HAVING first_event_at >= '2025-03-11 00:00:00'
        )
        SELECT 
          any(e.session_id) as id,
          any(e.project_id) as project_id,
          if(any(nullIf(e.profile_id, e.device_id)) IS NULL, any(e.profile_id), any(nullIf(e.profile_id, e.device_id))) as profile_id,
          any(e.device_id) as device_id,
          argMin(e.created_at, e.created_at) as created_at,
          argMax(e.created_at, e.created_at) as ended_at,
          if(
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = '',
            if(countIf(e.name = 'screen_view') > 1, true, false),
            argMaxIf(e.properties['__bounce'], e.created_at, e.name = 'session_end') = 'true'
          ) as is_bounce,
          argMinIf(e.origin, e.created_at, e.name = 'session_start') as entry_origin,
          argMinIf(e.path, e.created_at, e.name = 'session_start') as entry_path,
          argMaxIf(e.origin, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_origin,
          argMaxIf(e.path, e.created_at, e.name = 'session_end' OR e.name = 'screen_view') as exit_path,
          countIf(e.name = 'screen_view') as screen_view_count,
          0 as revenue,
          countIf(e.name != 'screen_view' AND e.name != 'session_start' AND e.name != 'session_end') as event_count,
          sumIf(e.duration, name = 'session_end') AS duration,
          argMinIf(e.country, e.created_at, e.name = 'session_start') as country,
          argMinIf(e.region, e.created_at, e.name = 'session_start') as region,
          argMinIf(e.city, e.created_at, e.name = 'session_start') as city,
          argMinIf(e.longitude, e.created_at, e.name = 'session_start') as longitude,
          argMinIf(e.latitude, e.created_at, e.name = 'session_start') as latitude,
          argMinIf(e.device, e.created_at, e.name = 'session_start') as device,
          argMinIf(e.brand, e.created_at, e.name = 'session_start') as brand,
          argMinIf(e.model, e.created_at, e.name = 'session_start') as model,
          argMinIf(e.browser, e.created_at, e.name = 'session_start') as browser,
          argMinIf(e.browser_version, e.created_at, e.name = 'session_start') as browser_version,
          argMinIf(e.os, e.created_at, e.name = 'session_start') as os,
          argMinIf(e.os_version, e.created_at, e.name = 'session_start') as os_version,
          argMinIf(e.properties['__utm_medium'], e.created_at, e.name = 'session_start') as utm_medium,
          argMinIf(e.properties['__utm_source'], e.created_at, e.name = 'session_start') as utm_source,
          argMinIf(e.properties['__utm_campaign'], e.created_at, e.name = 'session_start') as utm_campaign,
          argMinIf(e.properties['__utm_content'], e.created_at, e.name = 'session_start') as utm_content,
          argMinIf(e.properties['__utm_term'], e.created_at, e.name = 'session_start') as utm_term,
          argMinIf(e.referrer, e.created_at, e.name = 'session_start') as referrer,
          argMinIf(e.referrer_name, e.created_at, e.name = 'session_start') as referrer_name,
          argMinIf(e.referrer_type, e.created_at, e.name = 'session_start') as referrer_type,
          1 as sign,
          1 as version,
          argMinIf(e.properties, e.created_at, e.name = 'session_start') as properties
        FROM events e
        WHERE 
          e.session_id IN (SELECT session_id FROM unique_sessions)
          AND e.created_at BETWEEN '2025-03-11 00:00:00' AND '2025-03-15 00:00:00'
        GROUP BY e.session_id;