Hello, I am trying to construct an advanced query and running into some syntax issues. I’m trying to pull the latest meeting from a contact with the person names [[John Smith]] and the tag [[One on One]]. Here is my current query:
#+BEGIN_QUERY
{
:title [ :b "💡 Previous One on One meeting with John Smith" ]
:query (and [[One on One]] [[John Smith]])
:result-transform (fn [result] (take 1 result) )
}
#+END_QUERY
My issue is that this pulls the CURRENT one and one meeting, the one that I’m currently having. I’d like to pull the LAST one on one. So the match for the same query BEFORE today. Is there a way someone can help me with that? I keep getting errors when trying to add this function.
mentaloid, thank you for the response. I don’t know what I’m doing with the closure, but I get an invalid query error whenever I try to add these refinements. I don’t know if I’m applying them in the wrong order or what. Would you be able to walk me through it more?
We don’t know what you are doing either, and we cannot guess either, until you provide your effort in more detail, so as we have something specific to help you with in more detail.
{:title [ :h3 "💡 Previous One on One meeting with Sarah Langville" ]
:query [:find (pull ?b [*])
:in $ ?start ?today ?tag ?tag2
:where
[?b :block/page ?p]
[?p :page/journal-day ?d]
[(>= ?d ?start)]
[(< ?d ?today)]
[?b :block/refs ?ref]
(or [?ref :block/name ?tag]
[?ref :block/name ?tag2])
]
:inputs [:60d-before :today "[[Sarah Langville]]" "[[One on One]]"]}
)
The first issue I’m having is that if I change from the or operator, to the and operator, I get a weird error about "Missing rules var ‘%’ in :in, but I haven’t changed my variables.
The second issue I get is when I attempt to sort. When I apply a sort of the journal day, like so:
Okay thanks for that! I think I’ve got the sorting figured out, but my WHERE clause is still acting funny.
I seem to only be getting a single result, and I think I might be misunderstanding what I’m asking for. When I look at block/refs, does it include the child block as well?
The situation I have is something like this:
- Sarah and Evan #[[One on One]] Meeting
- [[Sarah Langville]]
- Notes 1
- Notes 2
and then others like this:
- [[Sarah Langville]]
tags: [[One on One]]
and neither are showing up. The only blocks I’m getting are when it’s all on one line, like so:
- Some Meeting
- Mention to [[Sarah Langville]] at our next [[One on One]]
Almost like the AND is only applying to the single block WITHIN the block, not a block group.
I’m almost there. I’ve got the sort working in reverse order, but my take 1 isn’t having any effect. I’m not sure if the take one needs to be appended to a different transform, or if I’m in the wrong order, or what. Here’s how my query stands now:
{
:title [ :h3 "💡 Previous One on One meeting with Sarah Langville" ]
:query [:find (pull ?b [*])
:in $ ?start ?today ?tag ?tag2
:where
;; filter by Journal Day
[?b :block/page ?p]
[?p :page/journal-day ?d]
[(>= ?d ?start)]
[(< ?d ?today)]
;; search for Tag and Tag2
[?b :block/path-refs ?ref] [?ref :block/name ?tag]
[?b :block/path-refs ?ref2] [?ref2 :block/name ?tag2]
]
;; reverse sort by day, take the first result
:result-transform (fn [result]
(take 1
(reverse
(sort-by (fn [h] (get h [:block/page :block/journal-day])))
)
)
)
:inputs [:-60d :today "[[Sarah Langville]]" "[[One on One]]"]
}
Would appreciate any insight into what I’m doing wrong here!
That is exactly it! Thank you very much for all the help on this query. For anyone else needing to do something similar, here is the result that I ended up with:
{
:title [ :h3 "💡 Previous One on One meeting with Sarah Langville" ]
:query [:find (pull ?b [*])
:in $ ?start ?today ?tag ?tag2
:where
;; filter by **Journal** **Day**
[?b :block/page ?p]
[?p :page/journal-day ?d]
[(>= ?d ?start)]
[(< ?d ?today)]
;; search **for** **Tag** **and** **Tag2**
[?b :block/path-refs ?ref] [?ref :block/name ?tag]
[?b :block/path-refs ?ref2] [?ref2 :block/name ?tag2]
]
;; sort by day desc, take the first result
:result-transform (fn [result]
(take 1
(sort-by (fn [h] (get h [:block/page :block/journal-day])) result)
)
)
:inputs [:-60d :today "[[Sarah Langville]]" "[[One on One]]"]
}