COUNT/DISTINCT queries

The kind of query you'd want to use for faceted browsing through articles in a journal, say.

MySQL:

SELECT `year`, COUNT(*) as c FROM `articles` WHERE `issn` = '1234-5678' GROUP BY `year` ORDER BY `year` ASC

XQuery (MarkLogic):

let $docs := doc()[/article/front/journal-meta/issn="1234-5678"]
for $year in fn:distinct-values($docs/article/front/article-meta/pub-date/year)
  let $count := count($docs[article/front/article-meta/pub-date/year = $year]) 
  order by $year ascending
  return
    <year c={$count}>{$year}</year>

(Note that this is really slow on large collections. Ideally you'd create a separate namespaced block for the issn and year metadata, and use that to build a lexicon for fast queries).

SPARQL:

FAIL