• 検索結果がありません。

SPARQL 集約

ドキュメント内 Semantics Developer’s Guide (ページ 110-113)

6.1 SPARQL を使用したトリプルのクエリ

6.1.13 SPARQL 集約

SPARQL

集計関数を使用すると、トリプルに対してシンプルな分析的クエリを実行でき

ます。集計関数は、トリプル内の値または値共起に対して操作を実行します。

例えば、集計関数を使用して、値の合計を計算できます。次の

SPARQL

クエリは、SUM を使用して総売上を調べます。

PREFIX demov: <http://demo/verb/>

PREFIX vcard: <http://www.w3.org/2006/vcard/ns/>

SELECT (SUM (?sales) as ?sum_sales)

FROM <http://marklogic.com/semantics/COMPANIES100/>

WHERE {

?company a vcard:Organization .

?company demov:sales ?sales }

サポートされている

SPARQL

集計関数は次に示すとおりです。

次の例は、大量のトリプルに対して集計関数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 .

集計関数 例

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 )

SAMPLE SELECT ?country ( SAMPLE( ?industry ) AS

?sample_industry ) ( SUM (?sales) AS ?sum_sales )

非集約変数を適切に評価するには、SAMPLEが必要です。

}=>

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

次のクエリは、集約(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:

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/ProductType4 59> .

?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

ドキュメント内 Semantics Developer’s Guide (ページ 110-113)