diff --git a/internal/sqldb/statements.go b/internal/sqldb/statements.go index 5eb8f5c5a..c0cfe0ae0 100644 --- a/internal/sqldb/statements.go +++ b/internal/sqldb/statements.go @@ -86,63 +86,72 @@ var statements = struct { ORDER BY date ASC; `, getStatVarSummaries: ` - WITH entity_types - AS (SELECT - o.variable AS variable, - t.object_id AS entity_type, - Count(DISTINCT o.entity) AS entity_count, - Min(o.value + 0.0) AS min_value, - Max(o.value + 0.0) AS max_value - FROM - observations o - JOIN triples t - ON o.entity = t.subject_id - WHERE - o.variable IN (:variables) - AND t.predicate = 'typeOf' - GROUP BY variable, entity_type - ORDER BY entity_count DESC), - entities - AS (SELECT - DISTINCT o.variable variable, - t.object_id entity_type, - t.subject_id entity_id - FROM - triples t - JOIN observations o - ON o.entity = t.subject_id - WHERE - t.predicate = 'typeOf' - AND t.object_id IN (SELECT entity_type FROM entity_types) - AND o.variable IN (SELECT DISTINCT variable FROM entity_types)), - sample_entities - AS (SELECT variable, entity_type, entity_id - FROM (SELECT - *, - Row_number() OVER (partition BY variable, entity_type) AS row_num - FROM entities) AS entities_with_row_num - WHERE row_num <= 3), - grouped_entities - AS (SELECT - variable, - entity_type, - Group_concat(entity_id) AS sample_entity_ids - FROM sample_entities - GROUP BY variable, entity_type), - aggregate - AS (SELECT - variable, - entity_type, - entity_count, - min_value, - max_value, - sample_entity_ids - FROM - entity_types - JOIN grouped_entities using(variable, entity_type)) + WITH entity_observations_triples AS ( + SELECT + o.variable, + t.object_id AS entity_type, + t.subject_id AS entity_id, + o.value + 0.0 AS numeric_value + FROM + observations o + JOIN + triples t ON o.entity = t.subject_id + WHERE + t.predicate = 'typeOf' + AND o.variable IN (:variables) + ), + entity_types AS ( + SELECT + variable, + entity_type, + COUNT(DISTINCT entity_id) AS entity_count, + MIN(numeric_value) AS min_value, + MAX(numeric_value) AS max_value + FROM + entity_observations_triples + GROUP BY + variable, entity_type + ), + sample_entities AS ( + SELECT + variable, + entity_type, + entity_id + FROM ( + SELECT + *, + ROW_NUMBER() OVER (PARTITION BY variable, entity_type ORDER BY entity_id) AS row_num + FROM + entity_observations_triples + ) AS entities_with_row_num + WHERE row_num <= 3 + ), + grouped_entities AS ( + SELECT + variable, + entity_type, + GROUP_CONCAT(entity_id ORDER BY entity_id) AS sample_entity_ids + FROM + sample_entities + GROUP BY + variable, entity_type + ), + aggregate AS ( + SELECT + et.variable, + et.entity_type, + et.entity_count, + et.min_value, + et.max_value, + ge.sample_entity_ids + FROM + entity_types et + JOIN + grouped_entities ge USING (variable, entity_type) + ) SELECT * - FROM aggregate; - `, + FROM aggregate; + `, getKeyValue: ` SELECT value FROM key_value_store