From 5bd80f6138e233241a1a63798121ee8631acc571 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Fri, 18 Oct 2024 19:23:09 +0200 Subject: [PATCH] add resulting join sqls from template in comment --- app/models/custom_field/order_statements.rb | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/app/models/custom_field/order_statements.rb b/app/models/custom_field/order_statements.rb index 464f3ed6b15c..a6811620cc02 100644 --- a/app/models/custom_field/order_statements.rb +++ b/app/models/custom_field/order_statements.rb @@ -92,6 +92,29 @@ def group_by_join_statement def can_be_used_for_grouping? = field_format.in?(%w[list date bool int float string link]) + # Template for all the join statements. + # + # For single value custom fields thejoin ensures single value for every + # customized object using DISTINCT ON and selecting first value by id of + # custom value: + # + # LEFT OUTER JOIN ( + # SELECT DISTINCT ON (cv.customized_id), cv.customized_id, xxx "value" + # FROM custom_values cv + # WHERE … + # ORDER BY cv.customized_id, cv.id + # ) cf_order_NNN ON cf_order_NNN.customized_id = … + # + # For multi value custom fields the GROUP BY and value aggregate function + # ensure single value for every customized object: + # + # LEFT OUTER JOIN ( + # SELECT cv.customized_id, ARRAY_AGG(xxx ORDERY BY yyy) "value" + # FROM custom_values cv + # WHERE … + # GROUP BY cv.customized_id, cv.id + # ) cf_order_NNN ON cf_order_NNN.customized_id = … + # def join_for_order_sql(value:, add_select: nil, join: nil, multi_value: false) <<-SQL.squish LEFT OUTER JOIN (