query-8df54be60bfed2460da536f388d8dec9
TODO
Use at
- https://query.wikidata.org/sparql
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
SELECT ?letter1 ?letter2 ?count (?count / ?total * 100 AS ?pct)
WHERE
{
{
SELECT ?letter1 ?letter2 (COUNT(*) AS ?count)
WHERE
{
?item wdt:P31 wd:Q5 . # Humans
VALUES ?countries { wd:Q38 wd:Q238 }. # Italy + San Marino
?item wdt:P27 ?countries . # Italians
?item wdt:P21 wd:Q6581097 . # male
# ?item wdt:P21 wd:Q6581072 . # female
?item p:P735 ?name1_statement . # Given name
?name1_statement ps:P735 / wdt:P1705 ?name1 . # Native label for name
OPTIONAL { ?name1_statement pq:P1545 ?ordinal1 . }
FILTER (! BOUND(?ordinal1) || ?ordinal1 = "1") # Use the only name (no ordinal value) or name #1
?item p:P734 ?name2_statement . # Family name
?name2_statement ps:P734 / wdt:P1705 ?name2 . # Native label for name
OPTIONAL { ?name2_statement pq:P1545 ?ordinal2 . }
FILTER (! BOUND(?ordinal2) || ?ordinal2 = "1") # Use the only name (no ordinal value) or name #1
BIND (SUBSTR(STR(?name1), STRLEN(?name1)) AS ?letter1)
BIND (SUBSTR(STR(?name2), STRLEN(?name2)) AS ?letter2)
}
GROUP BY ?letter1 ?letter2
}
{
SELECT (SUM(?count) AS ?total)
WHERE
{
{
SELECT ?letter1 ?letter2 (COUNT(*) AS ?count)
WHERE
{
?item wdt:P31 wd:Q5 . # Humans
VALUES ?countries { wd:Q38 wd:Q238 }. # Italy + San Marino
?item wdt:P27 ?countries . # Italians
?item wdt:P21 wd:Q6581097 . # male
# ?item wdt:P21 wd:Q6581072 . # female
?item p:P735 ?name1_statement . # Given name
?name1_statement ps:P735 / wdt:P1705 ?name1 . # Native label for name
OPTIONAL { ?name1_statement pq:P1545 ?ordinal1 . }
FILTER (! BOUND(?ordinal1) || ?ordinal1 = "1") # Use the only name (no ordinal value) or name #1
?item p:P734 ?name2_statement . # Family name
?name2_statement ps:P734 / wdt:P1705 ?name2 . # Native label for name
OPTIONAL { ?name2_statement pq:P1545 ?ordinal2 . }
FILTER (! BOUND(?ordinal2) || ?ordinal2 = "1") # Use the only name (no ordinal value) or name #1
BIND (SUBSTR(STR(?name1), STRLEN(?name1)) AS ?letter1)
BIND (SUBSTR(STR(?name2), STRLEN(?name2)) AS ?letter2)
}
GROUP BY ?letter1 ?letter2
} }
}}
ORDER BY DESC(?count)
Query found at
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
v12("?count"):::projected
v12("?countries")
v4("?item")
v12("?letter1"):::projected
v12("?letter2"):::projected
v7("?name1")
v6("?name1_statement")
v9("?name2")
v8("?name2_statement")
v3("?ordinal1")
v2("?ordinal2")
v13("?pct")
v12("?total"):::projected
a1((" "))
a2((" "))
a3((" "))
a4((" "))
c3(["wd:Q5"]):::iri
c6(["wd:Q6581097"]):::iri
f0[["(not bound(?ordinal2) || ?ordinal2 = '1')"]]
f0 --> v2
f1[["(not bound(?ordinal1) || ?ordinal1 = '1')"]]
f1 --> v3
v4 --"p:direct/P31"--> c3
bind2[/VALUES ?countries/]
bind2-->v12
bind20(["wd:Q38"])
bind20 --> bind2
bind21(["wd:Q238"])
bind21 --> bind2
v4 --"p:direct/P27"--> v12
v4 --"p:direct/P21"--> c6
v4 --"p:P735"--> v6
v6 --"p:statement/P735"--> a1
a1 --"p:direct/P1705"--> v7
subgraph optional0["(optional)"]
style optional0 fill:#bbf,stroke-dasharray: 5 5;
v6 -."p:qualifier/P1545".-> v3
end
v4 --"p:P734"--> v8
v8 --"p:statement/P734"--> a2
a2 --"p:direct/P1705"--> v9
subgraph optional1["(optional)"]
style optional1 fill:#bbf,stroke-dasharray: 5 5;
v8 -."p:qualifier/P1545".-> v2
end
bind3[/"substring(str(?name1),string-length(?name1))"/]
v7 --o bind3
bind3 --as--o v12
bind4[/"substring(str(?name2),string-length(?name2))"/]
v9 --o bind4
bind4 --as--o v12
bind6[/"count(*)"/]
bind6 --as--o v12
f7[["(not bound(?ordinal2) || ?ordinal2 = '1')"]]
f7 --> v2
f8[["(not bound(?ordinal1) || ?ordinal1 = '1')"]]
f8 --> v3
v4 --"p:direct/P31"--> c3
bind9[/VALUES ?countries/]
bind9-->v12
bind90(["wd:Q38"])
bind90 --> bind9
bind91(["wd:Q238"])
bind91 --> bind9
v4 --"p:direct/P27"--> v12
v4 --"p:direct/P21"--> c6
v4 --"p:P735"--> v6
v6 --"p:statement/P735"--> a3
a3 --"p:direct/P1705"--> v7
subgraph optional2["(optional)"]
style optional2 fill:#bbf,stroke-dasharray: 5 5;
v6 -."p:qualifier/P1545".-> v3
end
v4 --"p:P734"--> v8
v8 --"p:statement/P734"--> a4
a4 --"p:direct/P1705"--> v9
subgraph optional3["(optional)"]
style optional3 fill:#bbf,stroke-dasharray: 5 5;
v8 -."p:qualifier/P1545".-> v2
end
bind10[/"substring(str(?name1),string-length(?name1))"/]
v7 --o bind10
bind10 --as--o v12
bind11[/"substring(str(?name2),string-length(?name2))"/]
v9 --o bind11
bind11 --as--o v12
bind13[/"count(*)"/]
bind13 --as--o v12
bind15[/"sum(?count)"/]
v12 --o bind15
bind15 --as--o v12
bind16[/"?count / ?total * '100^^xsd:integer'"/]
v12 --o bind16
v12 --o bind16
bind16 --as--o v13