Grouping in Queries

I am a coding novice at best. I am trying to find a way to query tasks, grouping the results by tags. I am using a query straight from the LogSeq documentation. This is the query:

#+BEGIN_QUERY
{:title “next 7 days’ deadline or schedule”
:query [:find (pull ?block [*])
:in $ ?start ?next
:where
(or
[?block :block/scheduled ?d]
[?block :block/deadline ?d])
[(> ?d ?start)]
[(< ?d ?next)]]
:inputs [:today :7d-after]
:collapsed? false}
#+END_QUERY

It works fine, but the results have no organization that I can see. Is there any code I can add to organize the results by tags I have created? Even better would be to organize by tags and scheduled due date. I have tried several approaches based on similar questions here and in Discord but nothing has worked. Thank you for any help.

1 Like

I think that’s tricky. A workaround is to create a page/note for each tag, and then the following code will present tasks which are tagged with that note/page with scheduled tasks sorted in chronological order:

#+BEGIN_QUERY
{:title “:mag_right: :white_check_mark: TODOS tagged with :page_facing_up: current page sorted by :calendar: date scheduled”
:query [
:find (pull ?b [*])
:in $ ?current-page
:where
[?p :page/name ?current-page]
;; [?b :block/marker ?marker]
[?b :block/ref-pages ?p]
;; [(= “TODO” ?marker)]
;; [?block :block/scheduled ?d]
;; [?block :block/deadline ?d]
]
:result-transform (fn [result]
(sort-by (fn [d]
(get d :block/scheduled) ) result))
:inputs [:current-page ]}
#+END_QUERY

Hey!
Sorry for reviving this old thread. I have not learned anything about Datascript yet (it’s on my to-do list!) but I’d like to be able to use Logseq productively before finding time for learning…
What should be added to @Hulk’s answer to restrict the grouping to pages that have a specific property?

For example, I typically create TODOs in the journal page, but tagged with the page that it is relevant to:

Nov 2, 2021

  • [[Project 1]]
    • TODO Do this
    • TODO Do that [[Dev]] [[Python]]
  • TODO [[Project 2]] Do this also

Nov 1, 2021

  • TODO [[Project 1]] I should do that
  • [[Project 2]]
    • Problem A
      • TODO I should do this
    • Problem B
      • TODO find a solution

The pages [[Project 1]] and [[Project 2]] have the property “type”:“project”, which [[Dev]] and [[Python]] don’t have.
The standard query {{query (task todo)}} would list all TODOs grouped by journal page.
It’s not very clear to me what Hulk’s proposal is grouping by… But what I would love to have would look something like this:

[[Project 1]]

  • Nov 2, 2021 > Project 1
    • TODO Do this
    • TODO Do that [[Dev]] [[Python]]
  • Nov 1, 2021
    • TODO [[Project 1]] I should do that

[[Project 2]]

  • Nov 2, 2021
    • TODO [[Project 2]] Do this also
  • Nov 1, 2021 > Project 2 > Problem A
    • TODO I should do this
  • Nov 1, 2021 > Project 2 > Problem B
    • TODO find a solution

I’m not sure whether this makes sense, I guess I’ll adapt it when using it, but a little help to get started would greatly help :smiley: Thanks in advance!

3 Likes

My query sorted by date scheduled, but I couldn’t quite understand what you were aiming for…

A really powerful system would enable you to chain queries / operations together (I am used to working this way when using R to process data), e.g.

{{query [[projectA]]}} > {{filter -“meeting”}} > {{sortby date-created}}

Whether this kind of complex querying is computationally possible given the general design of logseq is another question.

2 Likes

This would be great indeed!
What I’m aiming for is to group the tasks by project (and listing only the tasks related to projects) before sorting them by date, do you see what I mean? So my list of tasks would not be a mishmash of all projects.
I could easily achieve something similar by querying a given project, but I don’t know in advance what projects I’ll have…

1 Like

@zigmhount curious, whether you were able to get desired query output from Logseq ?
I have a similar idea in mind which I’m having difficulties implementing in Logseq (in Obsidian as well, for that matter – I moved to Logseq because at first glance it looked promising and able to overcome Obsidian limitations, but now I’m getting slowly disillusioned…).

No, I didn’t continue that idea.

What I am doing instead is a query of all tasks on each project’s page using a template including <% current page %>:

{{query (and <% current page %>  (task TODO) )}}

Typically, I open the project’s page on the side view to see this todo list, and I continue working on the journal page. It’s also a lot less overwhelming than having a unique list of all projects’ tasks.
I do this a lot also with persons’ pages e.g. my colleagues which I tag on any TODO note, so that viewing that same query on their page gives me a list of what to discuss with them.