What I’m trying to do
I’m trying to get a better understanding of how dataview queries work. This may be a very trivial question, but it would be great to get clarification on that: am I understanding correctly, that a dataview queries generally operate at the level of pages/notes/files? By that I mean that the selection/filtering that is done with FROM
and WHERE
is always at the level of pages/notes/files so that if I want the query to return only specific elements from those notes, I need to specify those after LIST
or TABLE
?
The exception, of course, is TASK
queries, which operate directly at the level of tasks. So, my question does not concern these.
Side note: I would wish for an ITEM
query, which would target list-items directly, just like TASKS
targets tasks. It seems to be the logical continuation of the similarities between list-items and tasks described (and implied) in the documentation here: Metadata on Tasks and Lists - Dataview. Or perhaps this already exists, somehow?
Things I have tried
I started off with this query
```dataview
LIST filter(file.lists.text, (t) => contains(t, "#ai-history"))
FROM #ai-history
WHERE any(file.lists, (i) => contains(string(i), "#ai-history"))
```
It lists all list-items tagged with ai-history. But I didn’t like the redundancies in the queries and tried to simplify it:
```dataview
LIST file.lists.text
WHERE contains(file.lists.tags, "ai-history")
```
but this doesn’t work because it lists all list-items from every note that contains a list-item with the tag.
(Strangely, the removal of FROM #ai-history
also entails that an additional note is included. That additional note has a list-item with the ai-history tag, but the note itself doesn’t have that tag. This may be a bug.)
Anyway, here is the simplest version of the query I could come up with:
```dataview
LIST filter(file.lists.text, (t) => contains(t, "#ai-history"))
FROM #ai-history
```
What I don’t like about it, though, is that the actual query is done by the filter()
function rather than the query itself, if that makes any sense. I would find it more satisfying if I could state the conditions using WHERE
but that doesn’t seem to be possible because dataview queries select pages, not items. Is that correct?
I think the above is enough to explain my question, but here are some more thoughts, if you're interested.
Another imperfection with this query is that it doesn’t actually use actual tags when filtering, it just looks for text that looks like the tag. Fixing this is more an aesthetic challenge than anything else, but one way would be this
```dataview
LIST filter(file.lists.text, (t) => contains(t, "#ai-history"))
FROM #ai-history
WHERE contains(file.lists.tags, "ai-history")
```
But the added line is entirely redundant, so it makes no sense. So to avoid working with strings when I’m only interested in tags, I guess I would have to do
```dataview
LIST filter(file.lists, (f) => contains(f.tags, "ai-history")).text
FROM #ai-history
```
I then tried removing FROM #ai-history
but then I get an empty line for every note in my vault because the filter I specified after LIST
is only a display-filter, not a real query filter.
This kind of sums it up nicely: what bothers me is that I’m forced to use the display filter after LIST
to formulate my query, but then I can’t go all the way and use only that filter.