Query to show tasks linked to the current page which have a schedule date of today or earlier, or no schedule date at all

Hi,

I created the following query, which works but only shows tasks which have a scheduled date set, but how do I also include tasks that have no scheduled date at all?

#+BEGIN_QUERY
{:title "Tasks"
:query [:find (pull ?b [*])
    :in $ ?current-page ?today
    :where
    [?p :block/name ?current-page]
    [?b :block/refs ?p]
    [?b :block/scheduled ?d]
    [(<= ?d ?today)]
    (task ?b #{"TODO", "DOING"})]
:inputs [:current-page :today]}
#+END_QUERY

Any input would be helpful. Thanks! :slight_smile:

My idea is something like this, but can’t get it to work:

    (or (and [?b :block/scheduled ?d]
              [(<= ?d ?today)])
          (not [?b :block/scheduled ?d]))

So two things.

  1. we don’t want to check whether it has scheduled ?d, we want to check that it doesn’t have a value at all. So (not [?b :block/scheduled])
  2. we need an or-join here instead of an or. We are looking for a logical or as it’s called. We only need the variable ?d within our or statement. We don’t want to bind it with the rest of our query.
    Otherwise you basically had it :slight_smile: here’s the complete query.
#+BEGIN_QUERY
{:title "Tasks"
:query [:find (pull ?b [*])
    :in $ ?current-page ?today
    :where
    [?p :block/name ?current-page]
    [?b :block/refs ?p]
     (or-join [?b ?today] ; so or-join and bind both ?b and ?today
       (and 
         [?b :block/scheduled ?d]
         [(<= ?d ?today)]
       )
       (not [?b :block/scheduled])
     )
    (task ?b #{"TODO", "DOING"})
]
:inputs [:current-page :today]
}
#+END_QUERY

In my on-going effort to compile task management queries, I’ll link this topic:

2 Likes

Thank you so much, both for the solution and the explanation, it worked perfectly. :slight_smile:

I came here just to thank you for your helpful explanations and example queries that work. I am new to Logseq coming from Obsidian and your queries have been lifesavers. Not only that but your explanations have led me to write my own function queries. Thanks again!

3 Likes