0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【SPARQL】対象項目がないレコードを除外したい

Posted at

概要

以前に【SPARQL】レコード単位で保持されるデータを列の形式で取得したいで、外部結合のような機能を持つOPTIONAL句について紹介しました。外部結合に似た機能ですので、このOPTIONAL句で指定した項目がレコードにない場合もあります。そのようなとき、対象のレコードを除外したい場合どうすれば良いかというのをメモ書きします。

対応

Excluding Blank Nodes from SPARQL query resultsのstackoverflowの記事にある通り、isBlank関数を使えば空の項目を除外できそうです。OPTIONAL句で取得した項目を、FILTER句でisBlankで判定すればよさそうですね。

クエリサンプル

今回もgBizINFOの財務データを例にします。財務データでは売上高を取得できますが、登録されている会社とされていない会社があります。
以下は、売上高を登録されていない会社をisBlankの判定で除外し、結果を取得するクエリです。

PREFIX hj: <http://hojin-info.go.jp/ns/domain/biz/1#>
PREFIX ic: <http://imi.go.jp/ns/core/rdf#>
SELECT ?companyId ?companyName ?address ?salesAmount
FROM <http://hojin-info.go.jp/graph/zaimu>
WHERE {
  ?s hj:法人活動情報 ?o .
  ?o ic:ID/ic:識別値 ?companyId .
  ?o ic:名称/ic:表記 ?companyName .
  ?o ic:住所/ic:表記 ?address . 
  OPTIONAL {
    ?o hj:数量コレクション/hj:数量 ?c2 .
    ?c2 ic:数値 ?salesAmount .
    ?c2 hj:指標 ?salesAmountName .
    FILTER(?salesAmountName = "http://hojin-info.go.jp/code/財務情報/売上高"^^ic:コード型) 
  } 
 FILTER(!isBlank(?salesAmount))
}
ORDER BY ASC(?salesAmount)
LIMIT 10 
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?