query-cacf50682d88e1df9f93622889e72ac3

rq turtle/ttl

Grouping by itemsHello, As there are hundreds of people in these groups, I want to filter on several criteria. I want people connected to at least two groups or parties, excepted if it is only to political parties and no group, as several people are only changing parties and it is not interesting to include them. I also want some of the relatives of these people; the relatives appearing should be either connected to at least two of the “first people” themselves directly connected to groups (even if these “first people” are only connected to one group or party and therefore excluded from the first criteria), or be connected to one person connected to groups + connected to one group or party themselves. The explanations are probably quite confusing to read, there is a shorter FILTERing formula closer to the end of this post which makes the logic more understandable.), which I will call the “first people” or people from the first criteria, as well as the relatives they have in common (with a few different family properties).(P102)member of political party and political parties with (P463)member of I tried to make a graph of people related to some specific organisations (associations or less formal groups with I first went with a UNION to get people linked to groups and parties, and got different COUNTs on each, which we will call ?cG and ?cP, respectively for the number of groups and the number of political parties the person is connected with. For the relatives’ part, I went with a subquery reproducing the first UNION, added the relatives, and name the ?relative variable as the ?item variable in order to UNION this section with the first part and counted the connections to the relatives with ?cR. The further FILTER operations with the COUNTs will need to be done outside of the UNIONs, as it will be their different sums which will include or not the people in the final request. However I failed to properly write the UNIONs. I succeeded getting a result by having the two first COUNTs separated from the relatives’ one, but this way it does not group/merge the items in one, and the people appearing in both subqueries get two lines. This is now my main problem, all the rest can be ignored, I should be able to continue after solving this merging problem. The current request is:

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT DISTINCT ?item ?itemLabel (count (?memberOf) AS ?cG) (count (?memberOfPP) AS ?cP) ?cR WHERE {
  {
    VALUES ?memberOf { a bunch of wd:Q… }.
    ?item wdt:P463 ?memberOf.
  }
  UNION
  {
    VALUES ?memberOfPP { another bunch of wd:Q… }.
    ?item wdt:P102 ?memberOfPP.
  }
  UNION
  {
    SELECT (?relative AS ?item) (count (?relative) AS ?cR) WHERE {
      SELECT DISTINCT ?item ?relative WHERE {
        {
          VALUES ?memberOf { a bunch of wd:Q… again }.
          ?item wdt:P463 ?memberOf.
        }
        UNION
        {
          VALUES ?memberOfPP { another bunch of wd:Q… again }.
          ?item wdt:P102 ?memberOfPP.
        }
        ?relative ^(wdt:P22|wdt:P25|wdt:P26|wdt:P40|wdt:P451|wdt:P1038|wdt:P3373|wdt:P3448) ?item.
      }
    } GROUP BY ?relative
  }
  SERVICE wikibase:label {bd:serviceParam wikibase:language "fr,en". }
} GROUP BY ?item ?itemLabel ?cR

Query found at