The naming of my queries, which was somewhat bad, refers to the fact that the “FLATTEN example” does a FLATTEN file.tasks
, whilst the second doesn’t. And they should function almost identical, but they work in slightly different matters when it comes to how handle a single task, or counting stuff.
One of these differences comes related to having to resort some trickery like the sum(...)
in the second part. I’ll try to explain it with the following example of having three files with two tasks in each of the file.
Using the default theme, and these queries:
```dataview
TABLE file.tasks.text
WHERE file.folder = this.file.folder
AND startswith(file.name, "File")
```
```dataview
TABLE rows.file.name, rows.file.tasks.text
WHERE file.folder = this.file.folder
AND startswith(file.name, "File")
GROUP BY true
```
```dataview
TABLE rows.file.name, sum(rows.file.tasks.text)
WHERE file.folder = this.file.folder
AND startswith(file.name, "File")
GROUP BY true
```
We now get this output:
The first query, lists the two tasks related to each file within the same row (since we’ve not flattened the file.tasks
). When we then use GROUP BY true
(or similar) it’ll add these lists from each file into another list, which we can see by the double dots in the second query output.
The sum
function is used to reduce this list of lists, back into a single list of tasks, as shown in the third query. It’s the same happening in your query with the exception that there is so much other also going on. But the sum()
is used to reduce the list of lists into a single lists, which is then easier to do the filter
operation on, as we don’t need to reach into another list status (which is just painful to do).
Lastly, as can be seen in the previous query some trickery was needed to achieve that single lists of tasks when not flattening the file.tasks
. If we do use the FLATTEN file.tasks
on the other hand, we get the following queries:
```dataview
TABLE file.name, task.text
FLATTEN file.tasks as task
WHERE file.folder = this.file.folder
AND startswith(file.name, "File")
```
```dataview
TABLE rows.file.name, rows.task.text
FLATTEN file.tasks as task
WHERE file.folder = this.file.folder
AND startswith(file.name, "File")
GROUP BY true
```
Which outputs as
And here we see that in the first query, without grouping the result, we get just one task per row, but the file.name is doubled up in the two rows, since we’ve got two tasks per file.
When grouping this start list, we get the rows.task.text
at the wanted one-level list, easily accessible for filter
and company.
In summary, in most cases I’ll use the FLATTEN
on stuff like file.lists
or file.tasks
to make it easier to work with later on. If there are some reasoning demanding that I can’t FLATTEN
the list in the first place, then I’ll use sum(...)
to remove one level of the list before continuing to work with that list.
I presented both version to you, to give you the choice of either alternative, but in most cases I would use the FLATTEN
version, until it’s proven I can’t use it due to other demands of the surrounding queries.