ALTER TABLE dws_chat_assistant_day
    ADD COLUMN IF NOT EXISTS `tenant_id` UInt64 DEFAULT 0 AFTER `dimension_id`;

ALTER TABLE dws_chat_assistant_day DELETE WHERE `tenant_id` = 0;

INSERT INTO dws_chat_assistant_day
SELECT
    toDate(l.created) AS stat_date,
    l.assistant_id AS dimension_id,
    ifNull(s.tenant_id, toUInt64(0)) AS tenant_id,
    uniqExact(l.session_id) AS session_count,
    count() AS message_count
FROM ods_chat_log l
LEFT JOIN (
    SELECT id, tenant_id
    FROM ods_chat_session FINAL
) s ON s.id = l.session_id
GROUP BY stat_date, dimension_id, tenant_id;
