Automatic Query based on the parent block reference

Could use double negation:

(not                              ; and there is no
  [?parent :block/refs ?t2]       ; reference in the parent
  (not [?b :block/path-refs ?t2]) ; that does not also exist in the TODO
)

…and get:
image

2 Likes

@mentaloid @Siferiax
thanks both.
I will try both solutions

@Siferiax

In my case i have 3 references

[[Meeting]] with [[Siferiax]] and [[Fede]]

In the query I add the code

(not [?t1 :block/name "meeting"])

not to consider the page meeting.
How to get the TODO in common between Siferiax and Fede?

I tried this but it is not working

#+BEGIN_QUERY
{  :title [:h6 [:mark "QUERY"]]
:query [:find (pull ?b [*])
 :in $ ?parent
 :where
[?parent :block/refs ?t1]
[?parent :block/refs ?t2]
[?parent :block/refs ?t3]
[(!= ?t1 ?t2 ?t3)]
(not [?t1 :block/name "@meeting"])
(not [?t2 :block/name "@meeting"])
 (or-join [?b ?t1]
 [?b :block/path-refs ?t1]
 (and [?t1 :block/alias ?a]
 [?b :block/path-refs ?a]))
 (or-join [?b ?t2]
 [?b :block/path-refs ?t2]
 (and [?t2 :block/alias ?a]
 [?b :block/path-refs ?a]))
[?b :block/marker ?marker]
[(contains? #{"LATER" "NOW"} ?marker)]
(not [?var :block/refs ?b]
[?var :block/parent ?parent])
]
 :inputs [:parent-block]
:table-view? false
:result-transform (fn [result]
                (sort-by (fn [m]
                           (get m :block/marker)) > result))
 :breadcrumb-show? false
:collapsed? false
}
#+END_QUERY

Hi @mentaloid
If I add your code nothing happened.
Probably I need to add somehting more?

  • My code above was for Siferiax’ simplified example.
    • It works fine.
  • Your code has much more going on.
    • I don’t fully follow it.
      • For example, I don’t understand:
        • @meeting
        • ?var
        • the necessity of the overall complexity
      • One missed assumption is enough for the whole thing to fail.
        • Even by returning either:
          • no block at all
          • every single block
      • Considering the above, the following code produces the screenshot below, but may not work in your case for whatever reason.
    • It would really help if like Siferiax you could provide:
      • an example parent block
        • you have already provided that one
      • a list of existing blocks
        • like Siferiax’ last three blocks
      • a subset list of desired returned blocks
        • like Siferiax’ nested blocks
    • I have done the same in both cases.
#+BEGIN_QUERY
{:title [:h6 [:mark "QUERY"]]
 :query [:find (pull ?b [*])
   :in $ ?parent
   :where
     [?parent :block/refs ?t1]
     (or-join [?b ?t1]
       [?b :block/path-refs ?t1]
       (and
         [?t1 :block/alias ?a]
         [?b :block/path-refs ?a]
       )
     )

     (not
       [?b :block/path-refs ?meeting]
       [?meeting :block/name "meeting"]
     )
     (not                                ; and there is no
       [?parent :block/refs ?t2]         ; reference in the parent
       (not [?t2 :block/name "meeting"]) ; excluding its reference to meeting
       (not [?b :block/path-refs ?t2])   ; that does not also exist in the found block
     )

     [?b :block/marker ?marker]
     [(contains? #{"LATER" "NOW"} ?marker)]
     (not
       [?var :block/refs ?b]
       [?var :block/parent ?parent]
     )
 ]
 :inputs [:parent-block]
 :table-view? false
 :result-transform (fn [result]
   (sort-by (fn [m]
     (get m :block/marker)) > result
   )
 )
 :breadcrumb-show? false
 :collapsed? false
}
#+END_QUERY

image

  • This works for any number of references, without adding t3, t4 etc.
  • By the way, avoid using real usernames inside examples.

Hi @mentaloid

I tried to use your Query and this is the results

As you see “My Query” provides all the tasks for Leadership and Wellbeing individually and together, but “Your Query” does not provide anything to me, why?

This is the code of “My Query”

#+BEGIN_QUERY
{  :title [:h6 [:mark "My QUERY"]]
:query [:find (pull ?b [*])
 :in $ ?parent
 :where
 [?parent :block/refs ?t1]
(not [?t1 :block/name "@meeting"])
 (or-join [?b ?t1]
 [?b :block/refs ?t1]
 (and [?t1 :block/alias ?a]
 [?b :block/refs ?a]))
[?b :block/marker ?marker]
[(contains? #{"LATER" "NOW"} ?marker)]
]
 :inputs [:parent-block]
:table-view? false
:result-transform (fn [result]
                (sort-by (fn [m]
                           (get m :block/marker)) > result))
 :breadcrumb-show? false
:collapsed? false
}
#+END_QUERY

This is the code of “Your Query”

#+BEGIN_QUERY
{:title [:h6 [:mark "Your QUERY"]]
:query [:find (pull ?b [*])
 :in $ ?parent
 :where
   [?parent :block/refs ?t1]
   (or-join [?b ?t1]
     [?b :block/refs ?t1]
     (and
       [?t1 :block/alias ?a]
       [?b :block/refs ?a]
     )
   )
(not
     [?b :block/path-refs ?meeting]
     [?meeting :block/name "@meeting"]
   )
   (not                                ; and there is no
     [?parent :block/refs ?t2]         ; reference in the parent
     (not [?t2 :block/name "@meeting"]) ; excluding its reference to meeting
     (not [?b :block/path-refs ?t2])   ; that does not also exist in the found block
   )
[?b :block/marker ?marker]
   [(contains? #{"LATER" "NOW"} ?marker)]
]
:inputs [:parent-block]
:table-view? false
:result-transform (fn [result]
 (sort-by (fn [m]
   (get m :block/marker)) > result
 )
)
:breadcrumb-show? false
:collapsed? false
}
#+END_QUERY

Any help to understand why this is happening in my graph?

thanks a lot for the time you spend on this.

As I said:

In that case, the assumption was that the only excluded reference was that to @meeting. But in your example there is one more reference, that to @meeting/follow-up. Missing that returned no block at all. And we had no way to guess that by your previous posts. Therefore, should replace this:

(not [?t2 :block/name "@meeting"]) ; excluding its reference to meeting

…with this:

(not [?t2 :block/name "@meeting"]) ; excluding its reference to @meeting
(not [?t2 :block/name "@meeting/follow-up"]) ; and to @meeting/follow-up
1 Like

@mentaloid Thanks for taking the time to explaining to me.
It does work.

Most elegant solution :heart: will have to keep this in mind.