Skip to content

HAVING — post-aggregate FILTER

HAVING filters after grouping. Two equivalent shapes both work: referring to the alias, and inline aggregate.

What it does

A HAVING clause filters the post-aggregation result set — the SQL equivalent of HAVING rather than WHERE. pgRDF accepts both forms:

  1. Alias-referencedHAVING(?friends > 1) where ?friends was projected via AS.
  2. Inline aggregateHAVING(SUM(?v) > 10) where the aggregate is re-stated in the filter expression.

Why you'd use it

  • Data scientists — "predicates with more than N usages," "properties whose value-sum exceeds a threshold," answerable in a single query.

Example — alias-referenced

sql
SELECT * FROM pgrdf.sparql(
  'PREFIX foaf: <http://xmlns.com/foaf/0.1/>
   SELECT ?s (COUNT(?o) AS ?friends)
     WHERE { ?s foaf:knows ?o }
   GROUP BY ?s
   HAVING(?friends > 1)');

Example — inline aggregate

sql
SELECT * FROM pgrdf.sparql(
  'PREFIX ex: <http://example.com/>
   SELECT ?s
     WHERE { ?s ex:cost ?c }
   GROUP BY ?s
   HAVING(SUM(?c) > 1000)');

Tests

Apache-2.0 licensed. Documentation for pgRDF — built with VitePress, served via GitHub Pages.