How do I build a query that counts pages by category or subcategory?


Consider this simple situation:

I have a category called Parent Category with two sub categories called First Child and Second Child.

I have a Page A and a Page B that have the category First Child. I have a Page C has the category Second Child.

What I want to do is count the number of pages in each category. I can get a count of pages under the Parent Category: { { #ask: [ [Category: Parent Category] ] | format = count} }

I can produce a table of pages by category: { { #ask: [ [Category: Parent Category] ] | format = table} }

But, how do I do both so that I have a table that looks like this:

Category Count
first child 2
second child 1

I've tried a few things without success. Here is one example: { { #ask: [ [Category: Parent Category] ] | ?Category | ?count | format = table } }

Any advice you have would be appreciated.

19:55, 22 April 2015
  1. Make sure semantics is enabled for category pages,
  2. define Template:Count pages in category as {{#ask: [[Category:{{{1}}}]] | format = count}},
  3. insert query {{#ask: [[:Category:+]] [[Category:Parent category]] | link = none | format = template | template = Count pages in category}} into your page,
  4. submit and wait forever until the page is parsed.
03:00, 23 April 2015