6.1 SPARQL を使用したトリプルのクエリ
6.1.14 SPARQL 集約
<http://marklogic.com/semantics/mary> "John"
<http://marklogic.com/semantics/mary> "Mary"
また、cts:queryパラメータを使用して、クエリをコレクションまたはディレクトリ に制限することもできます。
SELECT (SUM (?sales) as ?sum_sales)
FROM <http://marklogic.com/semantics/COMPANIES100/>
WHERE {
?company a vcard:Organization .
?company demov:sales ?sales }
サポートされているSPARQL集計関数は次に示すとおりです。
集計関数 例
COUNT SELECT (COUNT (?company) as
?count_companies) Count of “companies”
SUM SELECT (SUM (?sales) as ?sum_sales)
MIN SELECT (MIN (?sales) as ?min_sales)
MAX SELECT ?country ( MAX (?sales) AS
?max_sales )
AVG SELECT ?industry ( ROUND( AVG
(?employees) ) AS ?avg_employees )
グループ化操作: GROUP BYでは、すべての集計関数がサポートされて います。
GROUP BY GROUP BY ?industry
または
GROUP BY ?country ?industry GROUP BY
<some_aggregate_
variable>
GROUP BY AVG
GROUP BY..HAVING
<some_aggregate_
variable>
GROUP BY ?industry
HAVING (?sum_sales > 3000000000 )
GROUP
CONCAT<more_than_one _item>
SELECT ?region
( GROUP_CONCAT( DISTINCT ?industry ; separator=" + " ) AS ?industries )
次の例は、大量のトリプルに対して集計関数COUNTを使用するSPARQLクエリを示し たものです。
PREFIX demor: <http://demo/resource/>
PREFIX demov: <http://demo/verb/>
PREFIX vcard: <http://www.w3.org/2006/vcard/ns/>
# count the companies
# (more precisely, count things of type organization) (SELECT ( COUNT (?company) AS ?count_companies )
FROM <http://marklogic.com/semantics/test/COMPANIES100/>
WHERE {
?company a vcard:Organization . }=>
100
次に、COUNTおよびORDER BY DESCを使用する別の例を示します。
PREFIX demor: <http://demo/resource/>
PREFIX demov: <http://demo/verb/>
PREFIX vcard: <http://www.w3.org/2006/vcard/ns/>
SELECT DISTINCT ?object (COUNT(?subject) AS ?count) WHERE {
?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type/> ?object
}
ORDER BY DESC (?count) LIMIT 10
SAMPLE SELECT ?country ( SAMPLE( ?industry ) AS
?sample_industry ) ( SUM (?sales) AS
?sum_sales )
非集約変数を適切に評価するには、SAMPLEが必要 です。
集計関数 例
次のクエリは、集約(MAX)を使用して、背番号が最も大きい野球選手を調べ、その選 手に関するすべてのトリプルを取得します。データセット内のすべての選手が持ってい ることが判明している任意のトリプル(bb:number)を使用し、主語を?keyに格納 します。その後、すべてのトリプルをクエリし、外側のクエリの主語が?key値にマッ チするものをフィルタリングします。
PREFIX bb: <http://marklogic.com/baseball/players/>
PEFIX bbr: <http://marklogic.com/baseball/rules/>
PREFIX xs: <http://www.w3.org/2001/XMLSchema#>
SELECT *
FROM <Athletics>
{
?s ?p ?o . {
SELECT(MAX(?s1) as ?key) WHERE
{
?s1 bb:number ?o1 . }
}
FILTER (?s = ?key) }
ORDER BY ?p
次のクエリは複雑な入れ子になっており、COUNT AVGを使用して、特定の製品タイプ のベンダーのうち、価格の低いほうから10件を調べます。製品タイプは、平均コスト を下回る製品のうち割合が最も高いものが選択されます。その後、「name1」または
「name2」が含まれるベンダーをフィルタリングします。
PREFIX bsbm: <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/vocabulary/>
PREFIX bsbm-inst: <http://www4.wiwiss.fu-berlin.de/
bizer/bsbm/v01/instances/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX cts: <http://marklogic.com/cts#>
SELECT ?vendor (xsd:float(?belowAvg)/?offerCount As
?cheapExpensiveRatio) {
{ SELECT ?vendor (count(?offer) As ?belowAvg) {
{ ?product a <http://www4.wiwiss.fu-berlin.de/bizer/
bsbm/v01/instances/ProductType459> .
?offer bsbm:product ?product .
?offer bsbm:vendor ?vendor .
?offer bsbm:price ?price .
{ SELECT ?product (avg(xsd:float(xsd:string(?price))) As ?avgPrice)
{
?product a <http://www4.wiwiss.fu-berlin.de/
bizer/bsbm/v01/
instances/ProductType459> .
?offer bsbm:product ?product .
?offer bsbm:vendor ?vendor .
?offer bsbm:price ?price . }
GROUP BB ?product }
} .
FILTER (xsd:float(xsd:string(?price)) < ?avgPrice) }
GROUP BY ?vendor }
{ SELECT ?vendor (count(?offer) As ?offerCount) {
?product a <http://www4.wiwiss.fu-berlin.de/
bizer/bsbm/v01/
instances/ProductType459> .
?offer bsbm:product ?product .
?offer bsbm:vendor ?vendor . }
GROUP BY ?vendor }
FILTER cts:contains(?vendor, cts:or-query(("name1",
"name2"))) }
ORDER BY desc(xsd:float(?belowAvg)/?offerCount) ?vendor LIMIT 10