From 33662f9f5e7302ec7ad5a2e5a9f4f93f265d2f7c Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Wed, 12 Feb 2025 11:26:40 -0800 Subject: [PATCH] join on website event for date ranges --- .../sql/sessions/getSessionDataValues.ts | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/queries/sql/sessions/getSessionDataValues.ts b/src/queries/sql/sessions/getSessionDataValues.ts index 3281521a..8cd6a4ab 100644 --- a/src/queries/sql/sessions/getSessionDataValues.ts +++ b/src/queries/sql/sessions/getSessionDataValues.ts @@ -27,11 +27,13 @@ async function relationalQuery( when data_type = 4 then ${getDateSQL('date_value', 'hour')} else string_value end as "value", - count(*) as "total" - from session_data - where website_id = {{websiteId::uuid}} - and created_at between {{startDate}} and {{endDate}} - and data_key = {{propertyName}} + count(distinct d.session_id) as "total" + from website_event e + join session_data d + on d.session_id = e.session_id + where e.website_id = {{websiteId::uuid}} + and e.created_at between {{startDate}} and {{endDate}} + and d.data_key = {{propertyName}} ${filterQuery} group by value order by 2 desc @@ -54,11 +56,13 @@ async function clickhouseQuery( multiIf(data_type = 2, replaceAll(string_value, '.0000', ''), data_type = 4, toString(date_trunc('hour', date_value)), string_value) as "value", - count(*) as "total" - from session_data final - where website_id = {websiteId:UUID} - and created_at between {startDate:DateTime64} and {endDate:DateTime64} - and data_key = {propertyName:String} + uniq(d.session_id) as "total" + from website_event e + join session_data d final + on d.session_id = e.session_id + where e.website_id = {websiteId:UUID} + and e.created_at between {startDate:DateTime64} and {endDate:DateTime64} + and d.data_key = {propertyName:String} ${filterQuery} group by value order by 2 desc