{"id":996,"date":"2020-02-09T13:22:33","date_gmt":"2020-02-09T13:22:33","guid":{"rendered":"http:\/\/wikidata.wwwnlsrc4.supercp.com\/?p=996"},"modified":"2020-11-25T17:24:26","modified_gmt":"2020-11-25T17:24:26","slug":"count","status":"publish","type":"post","link":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/modifying-results\/count\/","title":{"rendered":"COUNT"},"content":{"rendered":"\n<h2>COUNT<\/h2>\n\n\n\n<p>The aggregate function COUNT can be used to write queries to answer how many items there are that match a certain pattern. In such cases, we are not interested in the items that match our query pattern themselves, but just want to know the number of the items that match.<\/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 class=\"\" 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\" style=\"width:100%;max-width:100%;height:450px\" 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\u00a0<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 class=\"\" 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\" style=\"width:100%;max-width:100%;height:450px\" 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 The aggregate function COUNT can be used to write queries to answer how many items there are that match a certain pattern. In such cases, we are not interested in the items that match our query pattern themselves, but just want to know the number of the items that match. Suppose we wanted to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","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\/996"}],"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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/comments?post=996"}],"version-history":[{"count":26,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/posts\/996\/revisions"}],"predecessor-version":[{"id":1864,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/posts\/996\/revisions\/1864"}],"wp:attachment":[{"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/media?parent=996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/categories?post=996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wdqs-tutorial.toolforge.org\/index.php\/wp-json\/wp\/v2\/tags?post=996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}