From c924ef85825494237c351fa7c0aee392fb2a69f9 Mon Sep 17 00:00:00 2001 From: Bas Broekhuizen Date: Thu, 4 Sep 2025 15:46:29 +0200 Subject: [PATCH] fix: align clickhouse with prisma query --- .../sql/sessions/getWebsiteSessionStats.ts | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/queries/sql/sessions/getWebsiteSessionStats.ts b/src/queries/sql/sessions/getWebsiteSessionStats.ts index a2477c72..a3b86518 100644 --- a/src/queries/sql/sessions/getWebsiteSessionStats.ts +++ b/src/queries/sql/sessions/getWebsiteSessionStats.ts @@ -62,30 +62,30 @@ async function clickhouseQuery( sql = ` select sumIf(1, event_type = 1) as "pageviews", - uniq(session_id) as "visitors", - uniq(visit_id) as "visits", - uniq(country) as "countries", - sum(length(event_name)) as "events" + uniqIf(session_id, event_type = 1) as "visitors", + uniqIf(visit_id, event_type = 1) as "visits", + uniqIf(country, event_type = 1) as "countries", + sumIf(1, event_type = 2) as "events" from website_event ${cohortQuery} where website_id = {websiteId:UUID} - and created_at between {startDate:DateTime64} and {endDate:DateTime64} - ${filterQuery} - `; + and created_at between {startDate:DateTime64} and {endDate:DateTime64} + ${filterQuery} + `; } else { sql = ` select - sum(views) as "pageviews", - uniq(session_id) as "visitors", - uniq(visit_id) as "visits", - uniq(country) as "countries", - sum(length(event_name)) as "events" + sumIf(views, event_type = 1) as "pageviews", + uniqIf(session_id, event_type = 1) as "visitors", + uniqIf(visit_id, event_type = 1) as "visits", + uniqIf(country, event_type = 1) as "countries", + sumIf(1, event_type = 2) as "events" from website_event_stats_hourly website_event ${cohortQuery} where website_id = {websiteId:UUID} - and created_at between {startDate:DateTime64} and {endDate:DateTime64} - ${filterQuery} - `; + and created_at between {startDate:DateTime64} and {endDate:DateTime64} + ${filterQuery} + `; } return rawQuery(sql, params);