{"id":2748,"date":"2022-08-18T18:27:17","date_gmt":"2022-08-18T18:27:17","guid":{"rendered":"https:\/\/wdqs-tutorial.toolforge.org\/?p=2748"},"modified":"2023-04-06T17:13:09","modified_gmt":"2023-04-06T17:13:09","slug":"count","status":"publish","type":"post","link":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/modifying-results-it\/count\/?lang=it","title":{"rendered":"COUNT"},"content":{"rendered":"\n<h2>COUNT<\/h2>\n\n\n\n<p>La funzione di aggregazione COUNT pu\u00f2 essere utilizzata per scrivere query per rispondere a quanti elementi ci sono che corrispondono a un determinato modello. In questi casi, non siamo interessati agli elementi che corrispondono al nostro modello di query, ma vogliamo solo conoscere il numero degli elementi che corrispondono.<\/p>\n\n\n\n<p>Suppose we wanted to know how many Wikidata items there are about women chemists. In principle, we could run a query as follows:<\/p>\n\n\n\n<iframe style=\"width:100%;max-width:100%;height:450px\" src=\"https:\/\/query.wikidata.org\/#%23Women%20who%20are%20chemists%20on%20Wikidata%0ASELECT%20%3Fitem%20%3FitemLabel%20%0AWHERE%20%7B%0A%20%20%3Fitem%20wdt%3AP31%20wd%3AQ5.%20%20%20%20%20%20%20%20%20%20%20%20%23%20Item%20is%20instance%20of%20human%0A%20%20%3Fitem%20wdt%3AP21%20wd%3AQ6581072.%20%20%20%20%20%20%23%20Item%20is%20a%20woman%0A%20%20%3Fitem%20wdt%3AP106%20wd%3AQ593644.%20%20%20%20%20%20%23%20Occupation%20is%20chemist.%0A%20%20%0ASERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%22.%20%7D%20%20%0A%7D\" class=\"\" frameborder=\"0\"><\/iframe>\n\n\n\n<p>Above the table with the results, WDQS shows how many items were found in how many milliseconds of running the query.<\/p>\n\n\n\n<p>This strategy won&#8217;t always work, however. Suppose we wanted to know how many Wikidata items there are about actresses.  In principle, we could edit the query as follows: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror cm-s-eclipse\" data-setting=\"{&quot;mode&quot;:&quot;sparql&quot;,&quot;mime&quot;:&quot;application\/sparql-query&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true}\">#Actresses on Wikidata\nSELECT ?item ?itemLabel\nWHERE {\n  ?item wdt:P31 wd:Q5.            # Item is instance of human\n  ?item wdt:P21 wd:Q6581072.      # Item is a woman\n  ?item wdt:P106 wd:Q33999.       # Occupation is actor.     \n  \nSERVICE wikibase:label { bd:serviceParam wikibase:language &quot;[AUTO_LANGUAGE]&quot;. }  \n  }<\/pre><\/div>\n\n\n\n<p>In principle, WDQS would return all matching items and show us how many items were found that match the pattern. However, because the number of matching items is very large, the query will time out. This is due to a query deadline which is set to&nbsp;<strong>60 seconds<\/strong>.  Every query that takes more time to execute than this configured deadline will time out:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"363\" src=\"https:\/\/wdqs-tutorial.toolforge.org\/wp-content\/uploads\/2020\/11\/actress-timout-1024x363.png\" alt=\"\" class=\"wp-image-1859\" srcset=\"https:\/\/wdqs-tutorial.toolforge.org\/wp-content\/uploads\/2020\/11\/actress-timout-1024x363.png 1024w, https:\/\/wdqs-tutorial.toolforge.org\/wp-content\/uploads\/2020\/11\/actress-timout-300x106.png 300w, https:\/\/wdqs-tutorial.toolforge.org\/wp-content\/uploads\/2020\/11\/actress-timout-768x272.png 768w, https:\/\/wdqs-tutorial.toolforge.org\/wp-content\/uploads\/2020\/11\/actress-timout.png 1382w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>To answer how many actresses there are on Wikidata we declare a variable in the SELECT clause, in this example we call it ?actresscount. The  variable is defined as the COUNT of items with patterns matching those in the WHERE clause:<\/p>\n\n\n\n<iframe style=\"width:100%;max-width:100%;height:450px\" src=\"https:\/\/query.wikidata.org\/#%23Actresses%20on%20Wikidata%0ASELECT%20%28COUNT%28%3Fitem%29%20AS%20%3Factresscount%29%0AWHERE%20%7B%0A%20%20%3Fitem%20wdt%3AP31%20wd%3AQ5.%20%20%20%20%20%20%20%20%20%20%20%20%23%20Item%20is%20instance%20of%20human%0A%20%20%3Fitem%20wdt%3AP21%20wd%3AQ6581072.%20%20%20%20%20%20%23%20Item%20is%20a%20woman%0A%20%20%3Fitem%20wdt%3AP106%20wd%3AQ33999.%20%20%20%20%20%20%20%23%20Occupation%20is%20actor.%20%20%20%20%20%0A%20%20%0ASERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%22.%20%7D%20%20%0A%20%20%7D\" class=\"\" frameborder=\"0\"><\/iframe>\n\n\n\n<p>This query has only one result: the variable actresscount with the value of the count of all items matching the search pattern (human and female and actress)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>COUNT La funzione di aggregazione COUNT pu\u00f2 essere utilizzata per scrivere query per rispondere a quanti elementi ci sono che corrispondono a un determinato modello. In questi casi, non siamo interessati agli elementi che corrispondono al nostro modello di query, ma vogliamo solo conoscere il numero degli elementi che corrispondono. Suppose we wanted to know [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":""},"categories":[18],"tags":[],"_links":{"self":[{"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/posts\/2748"}],"collection":[{"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/comments?post=2748"}],"version-history":[{"count":1,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/posts\/2748\/revisions"}],"predecessor-version":[{"id":2749,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/posts\/2748\/revisions\/2749"}],"wp:attachment":[{"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/media?parent=2748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/categories?post=2748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/tags?post=2748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}