Code to query TODOs and SCHEDULEDs in the same time

Hi,

i have 2 kinds of TODO items:

  • normal TODO items.
  • some TODO with scheduled and repeaters, they were setup months ago, and they resurface regularly according to their repeaters.

right now, i am looking for code to query both of the TODOs say in the next 20 days …

as understood from many examples, it is easy to find code to query todos or schedules repectively, but so far i could not find codes to query both.

thank you for any hints

Might want to check out this topic:

If you need more help let me know!

1 Like

@Siferiax

hi, indeed i noticed the 2 posts from you, and guessed the trick is “or-join” structure …

i actually tried to cook up my code, (i agree the code looked not good)

i am still looking for the code

#+BEGIN_QUERY
{:title "all task & schedulers"
 :query [:find (pull ?h [*])
         :in $ ?next
         :where
         [?h :block/marker ?m]
         [(contains? #{"TODO"} ?m)]
         (or-join [?h ?d]
                  (and
                   [?h :block/ref-pages ?p]
                   [?p :block/journal? true]
                   [?p :block/jornal-day :d])
            [?h :block/scheduled ?d])
         [(< ?d ?next)]]
 :inputs [:+10d]
 :collapsed? false}
 }
#+END_QUERY

and it returns as below

I corrected the code below.

#+BEGIN_QUERY
{:title "all task & schedulers"
 :query [:find (pull ?h [*])
         :in $ ?next
         :where
         [?h :block/marker ?m]
         [(contains? #{"TODO"} ?m)]
         (or-join [?h ?d]
                  (and
                   [?h :block/refs ?p] ;ref-pages is now refs
                   [?p :block/journal? true]
                   [?p :block/journal-day ?d]) ;you had :d instead of ?d here and misspelling of journal
            [?h :block/scheduled ?d])
         [(< ?d ?next)]]
 :inputs [:+10d]
 :collapsed? false}
 }
#+END_QUERY
1 Like

@Siferiax thank you very much for your help.

i tried it, it returns schedulers, but only them, it cannot query regular TODO items, any hints?

thank you very much!

Right!
Please try this. I’m not 100% sure it is the correct filter for your use case. It depends on your data.
It should get either:

  • todo with scheduled
  • todo with reference to journal page
  • todo without scheduled and without reference to journal page
#+BEGIN_QUERY
{:title "all task & schedulers"
 :query [:find (pull ?h [*])
         :in $ ?next
         :where
         [?h :block/marker ?m]
         [(contains? #{"TODO"} ?m)]
         (or-join [?h ?next]
                  (and
                   [?h :block/refs ?p] ;ref-pages is now refs
                   [?p :block/journal? true]
                   [?p :block/journal-day ?d] ;you had :d instead of ?d here and misspelling of journal
                   [(< ?d ?next)]
             ) 
            (and 
              [?h :block/scheduled ?d]
              [(< ?d ?next)]
            )
            (not 
              [?h :block/scheduled]
              [?h :block/refs ?p]
              [?p :block/journal? true]
            )
         )
 ]
 :inputs [:+10d]
 :collapsed? false}
 }
#+END_QUERY
1 Like

@Siferiax this works great, nice fix !

i will look into the code, very nice

@Siferiax

one issue, the date specification not working, all todos & schedulers comes out

Could you be more specific?
What output do you expect and what do you get?

:inputs [:+10d]

it should return items in the next 10 days, and it actually returns all dates not 10 days

[(< ?d ?next)] says ?d is smaller than +10 days. +10 days from today.
So now you get everything that’s earlier than 5 March. (I think it’s 5 March)
So should add :today to :inputs.
And then using ?today in the :in
Then change [(< ?d ?next)] to [(< ?today ?d ?next)] in both places and add ?today to your or-join [?h ?next ?today]

the date ranger doesn’t work on me, i am checking the codes step by step

i broke down the code, and i found following option 1 does’t work from beginning, and option 2 works in a limited way

option 1:

#+BEGIN_QUERY
{:title "todos"
:query [:find (pull ?b [*])
        :in $ ?next
        :where
        [?b :block/marker ?m]
        [(contains? #{"TODO"} ?m)]
        [(< ?b ?next)]
        ]
 :inputs [:+10d]}
#+END_QUERY

above +10d or +20d, it doesn’t change anything, because it returns all

option 2: it works

#+BEGIN_QUERY
{:title "todos"
 :query [:find (pull ?b [*])
        :in $ ?start ?end
        :where
        [?b :block/marker ?m]
        [(contains? #{"TODO"} ?m)]
        [between ?b ?start ?end]
        ]
 :inputs [:today :+10d]}
#+END_QUERY

based on option 2, i updated the code, and it only returns TODO items (without scheduleds), and also date ranges not correctly

#+BEGIN_QUERY
{:title "all task & schedulers"
 :query [:find (pull ?h [*])
         :in $ ?start ?end
         :where
         [?h :block/marker ?m]
         [(contains? #{"TODO"} ?m)]
         (or-join [?h ?start ?end]
                  (and
                   [?h :block/refs ?p]
                   [?p :block/journal? true]
                   [?p :block/journal-day ?d]
                   [between ?d ?start ?end])
                  (and 
                   [?h :block/scheduled ?d]
                   [between ?d ?start ?end])
                  (not
                   [?h :block/scheduled]
                    ))]
 :inputs [:today :+10d]
 :collapsed? false}
 }
#+END_QUERY

Long story short, it was the (not ) clause that was the problem.
Your query should look like this I think?

#+BEGIN_QUERY
{:title "all task & schedulers"
 :inputs [:+10d :today]
 :query [:find (pull ?h [*])
 :in $ ?next ?today
  :where
   [?h :block/marker ?m]
   [(contains? #{"TODO"} ?m)]
   (or-join [?h ?next ?today]
     (and
       [?h :block/refs ?p]
       [?p :block/journal-day ?d] 
       [(< ?today ?d ?next)]
     ) 
     (and 
       [?h :block/scheduled ?d]
       [(< ?today ?d ?next)]
     )
     (and
       (not [?h :block/scheduled])
       (not
         [?h :block/refs ?p]
         [?p :block/journal? true]
       )
     )
   )
 ]
}
#+END_QUERY

hi,

i tried the code above, still it doesn’t work on me, the case is more complicated they expected, i will look into it carefully this week.

thank you for your help

Let me know if you need any more help. Just try to provide what result you’re looking for and what result you’re getting.

1 Like

@Siferiax

i currently run 2 separate codes to query TODOs and SCHEDULEs respectively, i know it is not very elegant, they work fine so far

thank you very much, i am still thinking about this code