Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 65 additions & 56 deletions internal/sqldb/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down