Automatic Query based on the parent block reference

Hi All,

I have a meeting template (block) where i can add the name of the person i have meeting with or the name of the project (always page).
I would like to have a query automatic to retrieve all the block with the tag follow up.

I use this query, but it is not working, any idea?

thanks
Fede

Could you post the query as text so I can copy it?

Thanks for the help.
Here’s the code in text


#+BEGIN_QUERY
{ :title [:h4 "Follow-up Actions" ] 
:query [:find (pull ?b [*])
 :in $ ?parent
 :where
 [?parent :block/refs ?thing]
[?t1 block/name "🔍follow-up" ]
 (or-join [?b ?thing ?t1]
 (and [?b :block/refs ?thing] [?b block/refs ?t1])
 (and [?person :block/alias ?a]
 [?b :block/refs ?a] [?b block/refs ?t1]))
]
 :inputs [:parent-block]
 :table-view? false
:result-transform (fn [result]
                (sort-by (fn [d]
                           (get d :block/scheduled) ) result))
 :breadcrumb-show? false
:collapsed? false
}
#+END_QUERY

Not sure what the problem is? The query works just fine.
Please add some example where it doesn’t work.

image

Hi.
I think the problem is that the parent-block has different reference pages not only #chiccoach but also #meeting. The TODO that are created inside a meeting block also they have #meeting reference, so when I query I get all the TODO.
I will try to find an example.

My question is if the parent block has two references #meeting and #chiccoach, the query will search the block with reference #meeting AND #chiccoach or #meeting OR #chiccoach?

Thanks a lot for taking the time to help me out

In my graph the problem is that the query basically gives me all the TODO with follow up, not only the one with #chiccoach.
But if I try to do the same in a new graph seems working…so I am a little lost

Your query has 2 seperate conditions for returning blocks.
It returns either those blocks that reference follow up (?t1) AND whatever the parent block references (for example meeting OR chiccoach) (?thing).
OR
those blocks that reference follow up (t1) AND the alias of an (arbitrary) page (?a from not otherwise specified page ?person)

Maybe the problem is that you see blocks because of that second part?

Thanks!!
the mistake was in the variable ?person I forgot to change it to ?thing

:rofl: :rofl: :rofl: :rofl: :rofl:

1 Like

Hi @Siferiax,

I have a problem the previous query works to fetch only the ones that have the reference inline [] or #.
e.g. LATER This is a task #Siferiax

But I cannot query the the ones like this

  • [[Siferiax]]
    • LATER This is a task

Is it possible to modify the code?

#+BEGIN_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]))

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

Change [?b :block/refs ?t1] to [?b :block/path-refs ?t1].
And change [?b :block/refs ?a] to [?b :block/path-refs ?a]

:block/refs is what the block itself refers to.
:block/path-refs includes references in it’s entire lineage (so parents, grandparents etc). It also includes the page the block is on, so keep that in mind.

1 Like

Thanks!!!
By the way it exists a way to do grouping in advance query?

Other than group by page option, no.

got is Thanks for the hint before!

1 Like

Hi all

Sometimes I have in my parent block 2 reference such as

  • [[meeting]] with [[John]]

With the automatic query that get the parent block refs I then collect all the block with meeting as reference or tags.
There is a way to consider only one reference?

Thanks

Not sure I understand the question?
Is the problem that you get everything with meeting and/or john? And you want only things with meeting?

Exaclty i give you an example

  • [[Meeting]] with [[Siferiax]]
    • Here is the query for parent block for markers TODO

I get all the TODO that has or MEETING or SIFERIAX, so basically all my TODO.
I would like to have only the one with SIFERIAX.
I tried to use (not (page-ref ?b "meeting")) in the query, but adding this I will not even get the TODO of SIFERIAX that I wrote down during a meeting.

I do not know how to solve.
The only way is to use only refs instead of path-refs, but in this way I lose all the nested TODO
e.g.

  • [[Sefirax]]
    • TODO Please help me!!!

Ok!
So this line: [?parent :block/refs ?t1] gives 2 results:
Id of page meeting.
Id of page Siferiax.

We do not care about the meeting page.

So after that line we need to add:
(not [?t1 :block/name "meeting"])
This would then exclude that result from the rest of the query.

1 Like

It works!!!
Is there something you do not know?

1 Like

Hi @Siferiax happy new year!!!
I have a question: the query for the parent block is perfect and can find the TODO based on the referenced page on the parent block.

If I have two referenced pages can find all todo for each pages. I am wondering if we can modify the query to make sure that the results is:

TODO with both pages referenced

basically if two pages are referenced I get only the todo in commons for both.

Is it possible?
Thanks

Happy new year!
I honestly don’t know if that is even possible. (Here see I don’t know everything :yum:)
To explain the clause [?parent :block/refs ?t1] is two results. Two rows.
If ?parent has an id of 1 and meeting 2 and Peter 3. The result would be.
[1 :block/refs 2]
[1 :block/refs 3]
As in two results. So then we ask that ?b also (path) references the same things. But that’s true for either result we just had.
So we can get [1 :block/refs 2] has a relation with ?b. So that gives us a result. Regardless of what the status of id 3 is.
You can see this if you use :find t1
Example:

So meeting is id 641 and Peter is id 1750.
You can see in the screenshot it is 2 results.

So dynamically this is not doable. If we know how many references there are then we can do something “stupid” like.

[?parent :block/refs ?t1]
[?parent :block/refs ?t2]
[(!= ?t1 ?t2)]
[?b :block/path-refs ?t1]
[?b :block/path-refs ?t2]

(I simplified it for clarity / ease of figuring stuff out)

That’s to the best of my knowledge.

1 Like