0

I've implemented a page with a long list of subojects. Every object contains one article (title + url) and N tags. I'd like to group by tag and show the count of articles related to that tag.

Something like:

SELECT tag, count(distinct article) 
GROUP BY tag

I found an answer but it's very generic and I'd also like to document the solution for other user with the same problem.

Revious
  • 7,816
  • 31
  • 98
  • 147
  • 1
    Install arrays extension. – IRA1777 Nov 23 '22 at 18:03
  • @IRA1777: done, but now? I've tried a search on google but the example I found seem quite unintuitive: https://www.semantic-mediawiki.org/wiki/Thread:Help_talk:Selecting_pages/Grouping_ORs_with_ANDs_in_arrays/reply – Revious Nov 23 '22 at 21:04

1 Answers1

1

As you know from previous answers to this question, you cannot have a "distinct" function from an SMW ask query. My preferred solution is to use the "arrays" extension, that allows you to access PHP array manipulation functions in wiki code. Further than "distinct" list of value, its an irreplaceable tool for handling semantic data from queries.

You can create an array with the following function :

{{#arraydefine: *identifier* | *data* | *delimiter* | *parameters* }}
  • Identifier is the variable name you want.
  • Data is the array content, in SMW context, you load it with a query result content.
  • Delimiter specify the array delimiter relative to data. This have to be coherent with the delimiter chosen in the ask query.
  • Parameters is where the magic appends. You can set a "unique" parameter, reducing the data list to unique values, thus, emulating the "distinct" function.

In tour case, you may do something like :

{{#arraydefine:tags 
| {{#ask:[[-Has subobject::{{FULLPAGENAME}}]] 
 |?Tags#-=
 | mainlabel=-
 |limit = 1000
}} 
|,
|unique
}}

Note that SMW ask query are, by default, limited to 50 results. Adding "limit=" adjusts the maximum result size.

At this point, you defined an array called "tags" containing all distinct values of this property.

You can use arrayprint function for any further data treatment or display.

IRA1777
  • 593
  • 3
  • 11
  • Thanks! I'm missing just a couple of point. What's the meaning of #-= (after Tags) and what's the difference with =- (after mainlabel) – Revious Nov 25 '22 at 18:20
  • 1
    # removes any potential link (html content) to get raw text – IRA1777 Nov 25 '22 at 20:14
  • 1
    #- I meant, as seen in documentation : https://www.semantic-mediawiki.org/wiki/Help:Displaying_information – IRA1777 Nov 26 '22 at 06:58