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:

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

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:

{: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
[?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 ]}

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!


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.

1 Like

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