Sort tasks per tag (often asked, never solved)

I am trying something again, after checking many posts. Nobody seems to be able to solve it.

In the journal I enter this:

Meeting 1 #customer1

  • text
  • TODO action 1
  • TODO action 2

Meeting 2 #customer2

  • text
  • TODO action 3
  • TODO action 4

Meeting 3

  • TODO action 5

Then I want to make a query in the journal template which shows all tasks, grouped by the block header tag. If no tag exists, then all the other tasks can be grouped together. This should result in the output:

#customer1

  • TODO action 1
  • TODO action 2

#customer 2

  • TODO action 3
  • TODO action 4

TODO action 5

In many task-apps this is standard behavior. In Logseq entering all in the Journal is great, but sorting the output you want with many tasks per block header tag.

Currently I use this query, which doesn’t group the tasks per block header tag and doesn’t display the tag:

#+BEGIN_QUERY
{:title "🔨 NOW, TODO or DOING"
:query [:find (pull ?h [*])
:in $ ?start ?today
:where
[?h :block/marker ?marker]
[(contains? #{"NOW" "TODO" "DOING"} ?marker)]
[?h :block/page ?p]
[?p :block/journal? true]
[?p :block/journal-day ?d]
[(>= ?d ?start)]
[(<= ?d ?today)]]
:inputs [:9999d :yesterday]
:result-transform (fn [result]
(sort-by (fn [h]
(get h :block/priority "Z")) result))
:group-by-page? true
:breadcrumb-show? true
:collapsed? false}
#+END_QUERY

Has anyone solved this? I am convinced that it must be possible.

1 Like

Try this:

#+BEGIN_QUERY
{
:title "Todos grouped by customer"
:query [:find (pull ?b [*])
:where
[?b :block/marker ?m]
[(= ?m "TODO")]]
:group-by-page? false
:result-transform
(fn [result]
(let [by-tag (group-by
(fn [b]
(let [tags (:block/tags b)]
(if (seq tags)
(clojure.string/join ", " tags)
"No Tag")))
result)]
(for [[tag blocks] by-tag]
[:div
[:h3 tag]
(for [b blocks]
[:div (first (:block/content b))])])))
}
#+END_QUERY

Thanks for this. Unfortunately the query gives an error (Logseq version 0.10.13). Can you try again?

Could you list the most relevant ones?

  • What makes you so sure?
  • It most probably isn’t possible.
    • At least not with queries alone.
    • Workarounds could still be possible.
      • e.g. sorting instead of grouping
  • It would help if you were specific about the given error.
  • I formatted the query to avoid errors on copying.
    • It will probably not produce the desired results anyway.
      • It looks like the output of an LLM.