@@ -19,9 +19,11 @@ defmodule Plausible.Stats.SQL.QueryBuilder do
1919 |> QueryOptimizer . split ( )
2020 |> Enum . map ( fn { table_type , table_query } ->
2121 q = build_table_query ( table_type , site , table_query )
22+ { table_type , q } |> IO . inspect ( label: :QUERY )
2223 { table_type , table_query , q }
2324 end )
2425 |> join_query_results ( query )
26+ |> IO . inspect ( label: :JOINED_QUERY )
2527 |> build_order_by ( query )
2628 |> paginate ( query . pagination )
2729 |> select_total_rows ( query . include . total_rows )
@@ -148,12 +150,20 @@ defmodule Plausible.Stats.SQL.QueryBuilder do
148150 Enum . reduce (
149151 session_only_dimensions ,
150152 q ,
151- & dimension_group_by ( & 2 , :events , :sessions , query , & 1 )
153+ & dimension_group_by_join ( & 2 , query , & 1 )
152154 )
153155 end
154156
155157 def build_group_by ( q , :sessions , query ) do
156- Enum . reduce ( query . dimensions , q , & dimension_group_by ( & 2 , table , query , & 1 ) )
158+ Enum . reduce ( query . dimensions , q , & dimension_group_by ( & 2 , :sessions , query , & 1 ) )
159+ end
160+
161+ defp dimension_group_by_join ( q , query , dimension ) do
162+ key = shortname ( query , dimension )
163+
164+ q
165+ |> Expression . select_dimension_from_join ( key , dimension )
166+ |> group_by ( [ ] , selected_as ( ^ key ) )
157167 end
158168
159169 defp dimension_group_by ( q , :events , query , "event:goal" = dimension ) do
@@ -178,9 +188,6 @@ defmodule Plausible.Stats.SQL.QueryBuilder do
178188 |> group_by ( [ ] , selected_as ( ^ key ) )
179189 end
180190
181- defp dimension_group_by ( q , base_table , join_table , query , dimension ) do
182- end
183-
184191 defp build_order_by ( q , query , { metric_or_dimension , order_direction } ) do
185192 order_by (
186193 q ,
0 commit comments