Query for tasks in namespace

I do SQL for a living, but in spite (or maybe because) of that I’m having a hard time wrapping my head around Logseq’s queries.

I’ll start with this. I’m trying to find all pages in my project namespace with active tasks. I can get this far:

:query [
:find (pull ?p [*])
:where 
[?b :block/marker ?t]
[(contains? #{"TODO", "DOING", "WAITING"}, ?t)]
[?b :block/page ?p]
[?p :block/namespace ?ns]
[?ns :block/name ?nsn]
]}

but I’m stuck limiting the results to pages in the right name space. For instance

:query [
:find (pull ?p [*])
:where 
[?b :block/marker ?t]
[(contains? #{"TODO", "DOING", "WAITING"}, ?t)]
[?b :block/page ?p]
[?p :block/namespace ?ns]
[?ns :block/name ?nsn]
[(contains? #{"project"} ?nsn)]
]}

returns no matches.

1 Like

What helped me with SQL to datalog is to keep in mind that datalog where clauses are based on table form.
So

select *
from table
where column = value

Becomes

{:query [:find (pull ?id [*]) ;this is the select, get all attribute (columns) for this ?id
 :where
   [?id column value]
 ]
}

As datalog just has 1 table, there is no from.
The table has a number of columns, but you generally use 3.
The first is the id (of the entity, think of this as the table name), the second the attribute (column) name and the third the attribute (column) value.
The value can be the id of another entity in the table.
We can substitute the actual values in the database for either a variable (?name) or a _ to signal any value.

The second query you posted will find pages under namespace “project” but only the ones directly below. So project/page 1, but not project/page 1/page 2.

Also in "WAITING"}, ?t)] there’s a comma that doesn’t belong there.

How can I make this query kind of recursive to accept everything withing that namespace, even if there are nested namespaces?

i.e: How can I get all TODOs in the Classes namespace, even tasks under Classes/2023/Operating Systems

See this topic for some ideas:

I haven’t gotten back to that discussion, but hopefully it suits your use case.