Query for tasks and sub-tasks without other blocks

Hello, in another old post, I found a query to have a task page with a list of tasks and sub-tasks.

{:query [:find (pull ?b [*])
        :in $ %
        [?b :block/marker ?marker]
        [(contains? #{"TODO"} ?marker)]
        ;; ancestor is true when ?ancestor is an ancestor of b
        (not (ancestor ?b ?ancestor)
             [?ancestor :block/marker ?amarker]
             [(contains? #{"TODO" "NOW" "DOING"} ?amarker)])]
    [[[[ancestor ?b ?ancestor]
				[?b :block/parent ?ancestor]]
				[[ancestor ?b ?ancestor]
				[?child :block/parent ?ancestor]
				(ancestor ?b ?child)]]]
:result-transform (fn [result]
                    (sort-by (fn [b]
                               (get b :block/priority "Z")) result))
:breadcrumb-show? false

I have a problem: I would like only the child blocks that are TODO to appear because currently, both TODO blocks and their non-TODO children appear, and I only want the sub-tasks.
Thank you very much :slight_smile:

First things first, a block in terms of query results always includes its children.

So if a block with children matches the query criteria, it will show with its children.

An option around this would be to always fold the results. Either by determining if the block has children, or by just simply collapsing all blocks.

The first post of that thread has the unconditional variant.