简体   繁体   English

SPARQL仅限制结果三元组中的特定变量

[英]SPARQL limit only a specific variable in a result triple

I try to get a SPARQL result with only the highest variable for a triple combination. 我尝试使用三元组合的最高变量获得SPARQL结果。

Ok, let's try to explain it. 好的,让我们尝试解释一下。

Let's say we got something like this: 假设我们得到了这样的东西:

:resource1 pred:hasQuality :UltraQuality
:resource2 pred:hasQuality :MediumQuality
:resource2 pred:hasQuality :LowQuality
:resource3 pred:hasQuality :UltraQuality
:resource3 pred:hasQuality :LowQuality
:resource4 pred:hasQuality :LowQuality

Now, I want to get all resources with there highest quality. 现在,我想在那里获得最高质量的所有资源。 The quality could be ordered with ORDER BY or MAX. 可以使用ORDER BY或MAX订购质量。 (L->M->U). (L-> M-> U)。

I tried a lot like this: 我做了很多这样的尝试:

SELECT ?res ?qual
WHERE {
    ?res pred:hasQuality ?qual.
}
ORDER BY ?res DESC(?qual)

But now, if there are more than one triple for the same ?res, I want to eliminate the other ones. 但是现在,如果相同的轮胎有三个以上,我想消除其他三个。

The expected result should be: 预期结果应为:

:resource1 :UltraQuality
:resource2 :MediumQuality
:resource3 :UltraQuality
:resource4 :LowQuality

You want to group by the items, which provides you with a set of qualities for each item. 您想group by项目group by ,这为每个项目提供了一组质量。 From that item, you want to take the maximum value. 您要从该项目中获取最大值。 Suppose you've got this data: 假设您有以下数据:

@prefix : <http://stackoverflow.com/q/20562673/1281433/>

:a :quality 3 .
:a :quality 4 .
:a :quality 5 .

:b :quality 9 .

:c :quality 2 .
:c :quality 1 .

Then you can use a query like this: 然后,您可以使用如下查询:

prefix : <http://stackoverflow.com/q/20562673/1281433/>

select ?item (max(?quality) as ?maxQuality)
where {
  ?item :quality ?quality .
}
group by ?item

You'll get these results: 您将得到以下结果:

---------------------
| item | maxQuality |
=====================
| :b   | 9          |
| :c   | 2          |
| :a   | 5          |
---------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM