简体   繁体   中英

Semantic Mediawiki: aggregation similar to SQL GROUP BY like #ask query

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM