I am trying to have notes that share tags displayed in notes.
I use the following to accomplish that, but there is one problem I would like to solve.
TABLE WITHOUT ID
row.file.link as Note, tag as Tags
FLATTEN file.tags as Tag
!contains(file.path,this.file.path) AND econtains(this.file.tags,Tag)
This would currently display the same notes multiple times if they share multiple tags.
Is there a way to let it not show the same similar notes multiple times?
I tried GROUP BY row.file.link, but that made the row.file.links appear invalid.
You’ve got to choose what you want to display when doing stuff like this. If the rows in your table is one row for each tag, then the files having multiple matching tags will be duplicated. If the rows are one for each file, then you’ll be duplicating the list of the matching tags.
So which part do you want to duplicate in the row, and what do you want to focus on in each row of your table?
No need to apologize, it’s just a rather specific request which needed a little clarification. So try the following query:
TABLE WITHOUT ID file.link as Note, common, unique
FLATTEN array(filter(file.etags, (f) => econtains(this.file.etags, f))) as common
FLATTEN array(filter(file.etags, (f) => !econtains(this.file.etags, f))) as unique
WHERE length(common) > 0
The trick to this query is that both the origin file and the candidates may have multiple tags, so you’ll need to do a double loop. In the query this is accomplished through doing a filter() for one loop, and the econtains() for the other loop. To preserve the output from the filter as a list, the expression is surrounded by an array() function.
The second FLATTEN producing the unique is not needed, it’s just for debug purposes, and can be removed if you want to.
The WHERE clause can be simplified into WHERE common or WHERE length(common), but doing WHERE length(common) > 0 has a higher readability and showcases what’s actually happening on that line.
Bonus tip: How to present code properly in a forum post
If you want to showcase either markdown, or code blocks, or dataview queries properly in a forum post, be sure to add one line before and one life after what you want to present with four backticks, ````. This will ensure that any other backticks (like for code blocks or queries) is properly shown.