Tagged blocks unless with tagged child

I am marking some blocks with #q in my graph, how do I query for all blocks marked with #q that do not contain #a in one of their children?
This one

{{query (and [[q]] (not [[a]]))}}

returns all blocks with #q and excludes blocks only if they are marked #a on the same level. How do I filter out those if a child is marked #a as well?

The following advanced query works for me.
I’m trying to find out how to add an extra tag that is the parent of the #q.

:query [:find (pull ?b [*])
[?b :block/refs [:block/name “q”]]
(not-join [?b]
[?child :block/parent ?b]
[?child :block/refs [:block/name “a”]]

What do you mean by “an extra tag that is the parent of the #q” ? Could you provide one or more examples?

In the daily journal pages I track i.e. meetings, calls, etc. They are in the below format.
For project queries I would like to have the results that are only related to the project.

The above query gives all unanswered questions for #project_a and #project_b. So the questions is, how to extend the query to show only only the unanswered questions of #project_a.

  • Meeting: Discuss possibilities of queries #project_a

    • Random note, queries are nice.
    • #Q How do I make an query finding all unanswered questions related to #project_a?
    • #Q How can you define queries?
      • #A With the query builder.
    • What about questions that are a level deeper.
      • #Q The parent block does not have a tag #project_a. How to include this?
  • Meeting: Explore note taking #project_b

    • Note taking apps: Logseq, Notepad
    • #Q Is there a comparison of all note taking apps?
    • #Q Where can you find help about Logseq?

If you want that query on the project page, you can use inputs query-page.

Then you would get this:

{:inputs [:query-page]
 :query [:find (pull ?b [*])
  :in $ ?qpage
   [?qp :block/name ?qpage]
   [?b :block/path-refs ?qp]
   [?b :block/refs [:block/name "q"]]
   (not-join [?b]
     [?child :block/parent ?b]
     [?child :block/refs [:block/name "a"]]

path-refs effectively means the reference is in block ?b or any of its parents.

I was building the query outside the project page so I wonder how the query looks outside the project page (if possible at all).

Your example didn’t work until I realized that my page name has a namespace (project/A) and I use an alias (project_a) when tagging. When I used the page name instead of the alias your query worked.

So it seems you can’t query aliases. I might have to re-think how to setup my notes now.

Change :query-page that’s in the inputs to "project/a" (has to be lower case) and you’re good to go.

That’s totally possible. We just need to change the query a little :slight_smile:

Change this part [?b :block/path-refs ?qp] to these lines:

[?qp :block/alias ?qa]
  [?b :block/path-refs ?qp]
  [?b :block/path-refs ?qa]

Now it will work in both cases.

1 Like