I’m going to gather some queries here that will be useful for task management.
It won’t be done in one go and I will be adding inline comments to help you understand what is going on. I have added those to build on the queries in order. So read through them all as I have only added comments the first time a statement appeared.
Hopefully this will help you be able to edit the queries for yourself.
Tasks past due
Tasks scheduled value is past due (based on today)
#+BEGIN_QUERY
{:title [:h3 "🔥 Past scheduled"]
:query [:find (pull ?b [*])
:in $ ?day ; ?day is the name for the first value in inputs further down.
:where
[?b :block/marker "TODO"] ; Using TODO straight in the clause because I want marker to be a specific value.
[?b :block/scheduled ?d] ; the block ?b has attribute scheduled with value ?d
[(< ?d ?day)] ; the value ?d is smaller than the value ?day
]
:inputs [:today] ; use the Logseq dynamic variable :today as input for this query (gives today's date as yyyymmdd format)
:table-view? false
}
#+END_QUERY
Open scheduled tasks
#+BEGIN_QUERY
{:title [:h3 "✅ Planned"]
:query [:find (pull ?b [*])
:where
[?b :block/marker ?marker]
[(contains? #{"TODO" "LATER"} ?marker)] ; TODO put in a value list with LATER.
[?b :block/scheduled ?d] ; ?b has attribute scheduled with value ?d, ?d is not further specified and so is any value. The same can be accomplish with _
]
:result-transform (fn [result] (sort-by (fn [r] (get-in r [:block/scheduled])) result)) ; sort the result by the scheduled date
:table-view? false
:breadcrumb-show? false ; don't show the parent blocks in the result !important, due to result-transform the grouping is lost, and so you will be left with a simple list of TODO items. having those parents blocks mixed in may make the list more confusing. (setting this to true won't show the page btw!)
:collapsed? false
}
#+END_QUERY
Open unplanned tasks from journal pages
#+BEGIN_QUERY
{:title [:h3 "☑ Unplanned"]
:query [:find (pull ?b [*])
:in $ ?day
:where
[?p :block/journal-day ?d] ; ?p has the attribute journal-day with value ?d (you don't need the :block/journal? attribute if you also use this one)
[(< ?d ?day)]
[?b :block/page ?p] ; ?b has the attribute :block/page with value ?p, ?p has been define with the identifier of a journal page above.
[?b :block/marker "TODO"]
(not [?b :block/scheduled _]) ; ?b doesn't have the attribute scheduled (_ is used to say that the value doesn't matter. If a value is specified it would read as ?b may have the attribute, but not with that value)
]
:result-transform (fn [result] (sort-by (fn [r] (get-in r [:block/page :block/journal-day])) result)) ; Sort by the journal date
:inputs [:today]
:table-view? false
:breadcrumb-show? false
:collapsed? false
}
#+END_QUERY
Yesterday’s open tasks
Tasks open from yesterday’s journal page, that don’t have a deadline/scheduled date or their deadline/scheduled date was yesterday.
#+BEGIN_QUERY
{:title [:h3 "🔥 Yesterday's open tasks"]
:query [:find (pull ?b [*])
:in $ ?day
:where
[?p :block/journal-day ?day] ; Here we input the value of the input into the clause immediately as it is an = statement.
[?b :block/page ?p]
[?b :block/marker "TODO"]
(or
[?b :block/scheduled ?day] ; Either the scheduled value is equal to ?day
(not [?b :block/scheduled]) ; or the scheduled attribute doesn't exist (_ is omitted here, it is instead implied)
)
(or
[?b :block/deadline ?day] ; same as above, but for the deadline attribute.
(not [?b :block/deadline])
)
]
:inputs [:yesterday]
:table-view? false
}
#+END_QUERY
Deadline to be scheduled
Not yet scheduled with deadline on certain day (I use the coming Sunday as a date)
#+BEGIN_QUERY
{:title [:h3 "🎯 Not yet planned"]
:query [:find (pull ?b [*])
:in $ ?day
:where
[?b :block/deadline ?d]
[(<= ?d ?day)]
[?b :block/marker "TODO"]
(not [?b :block/scheduled _])
]
:result-transform (fn [result] (sort-by (fn [r] (get-in r [:block/deadline])) result))
:breadcrumb-show? false
:table-view? false
:inputs [20230212]
}
#+END_QUERY
Scheduled with deadline
Planned, with deadline on certain date (again I use the next Sunday)
#+BEGIN_QUERY
{:title [:h3 "🎯 Planned"]
:query [:find (pull ?b [*])
:in $ ?day
:where
[?b :block/deadline ?d]
[(<= ?d ?day)]
[?b :block/marker "TODO"]
[?b :block/scheduled _]
]
:result-transform (fn [result] (sort-by (juxt (fn [r] (get-in r [:block/scheduled])) (fn [r] (get-in r [:block/deadline])) ) result)) ; Sort first by scheduled and then by deadline.
:breadcrumb-show? false
:table-view? false
:inputs [20230212]
}
#+END_QUERY
Todo in sub block
Sorting options for simple deadline/scheduled query
Tasks that have specific page reference
#+BEGIN_QUERY
{:title [:h3 "Tasks with page reference" ]
:query [:find (pull ?b [*])
:where
[?p :block/name "page"] ; name is always lowercase
[?b :block/refs ?p] ; this block references ?p as oppose to being on ?p through :block/page.
[?b :block/marker "TODO"]
]
}
#+END_QUERY
Tasks without a specific page link
#+BEGIN_QUERY
{:title [:h3 "Tasks without page reference" ]
:query [:find (pull ?b [*])
:where
[?p :block/name "page"] ; name is always lowercase
[?b :block/marker "TODO"]
(not [?b :block/refs ?p]) ; we cannot use not until we have specified the variables used in it
]
}
#+END_QUERY
Unscheduled non-project tasks
Tasks that are not scheduled and not on a page with tags:: projecten
(this property creates page links)
#+BEGIN_QUERY
{:title [:h3 "🔔 Ongeplande taken"]
:query [:find (pull ?b [*])
:where
[?b :block/marker "TODO"]
(not [?b :block/scheduled])
[?b :block/page ?p]
[?pr :block/name "projecten"]
(not [?p :block/tags ?pr])
]
:table-view? false
}
#+END_QUERY
Tasks by current page & it’s aliases
This uses a more complex or
statement. This is basically a logical or. it is needed here as variable ?a
is only for use in the or
. [?b ?p]
means these two variables are bound with the rest of the query.
More information: https://docs.datomic.com/on-prem/query/query.html#or-join-clause
#+BEGIN_QUERY
{:title ["Query by page & alias"]
:query [:find (pull ?b [*])
:in $ ?page
:where
[?b :block/marker "TODO"]
[?p :block/name ?page]
(or-join [?b ?p]
[?b :block/refs ?p]
(and [?p :block/alias ?a] ; this attribute is available only when alias:: has been specified as a page property.
[?b :block/refs ?a])
)
]
:result-transform :sort-by-priority
:table-view? false
:inputs [:current-page] ; alternatively use :query-page to use the page the query is on, rather than the page you are viewing
}
#+END_QUERY
Suggestions welcome! Also do link to other threads on the forum with similar questions. Let’s make this a task management resource for everyone to easily find and reference!