diff --git a/lib/cartodb/models/dataview/aggregation.js b/lib/cartodb/models/dataview/aggregation.js index b1e56f28..d51edf04 100644 --- a/lib/cartodb/models/dataview/aggregation.js +++ b/lib/cartodb/models/dataview/aggregation.js @@ -142,6 +142,34 @@ const aggregationDataviewQueryTpl = ctx => ` ${!!ctx.override.ownFilter ? `${aggregationQueryTpl(ctx)}` : `${rankedAggregationQueryTpl(ctx)}`} `; +const filterCategoriesQueryTpl = ctx => ` + SELECT + ${ctx.column} AS category, + ${ctx.value} AS value + FROM (${ctx.query}) _cdb_aggregation_search + WHERE CAST(${ctx.column} as text) ILIKE ${ctx.userQuery} + GROUP BY ${ctx.column} +`; + +const searchQueryTpl = ctx => ` + WITH + search_unfiltered AS ( + ${ctx.searchUnfiltered} + ), + search_filtered AS ( + ${ctx.searchFiltered} + ), + search_union AS ( + SELECT * FROM search_unfiltered + UNION ALL + SELECT * FROM search_filtered + ) + SELECT category, sum(value) AS value + FROM search_union + GROUP BY category + ORDER BY value desc +`; + const CATEGORIES_LIMIT = 6; const VALID_OPERATIONS = { @@ -316,31 +344,3 @@ module.exports = class Aggregation extends BaseDataview { }); } }; - -const filterCategoriesQueryTpl = ctx => ` - SELECT - ${ctx.column} AS category, - ${ctx.value} AS value - FROM (${ctx.query}) _cdb_aggregation_search - WHERE CAST(${ctx.column} as text) ILIKE ${ctx.userQuery} - GROUP BY ${ctx.column} -`; - -const searchQueryTpl = ctx => ` - WITH - search_unfiltered AS ( - ${ctx.searchUnfiltered} - ), - search_filtered AS ( - ${ctx.searchFiltered} - ), - search_union AS ( - SELECT * FROM search_unfiltered - UNION ALL - SELECT * FROM search_filtered - ) - SELECT category, sum(value) AS value - FROM search_union - GROUP BY category - ORDER BY value desc -`;