Queries for task management

Not that I’m aware of.
Though with :group-by-page? false (newest Logseq version) and :breadcrumb-show? false you should just get a list of tasks.

Full query:

#+BEGIN_QUERY
{:title "List of tasks"
 :query [:find (pull ?b [*])
   :where
     [?b :block/marker ?m]
     (not [(contains? #{"DONE" "CANCELED"} ?m)] )
     (not 
       [?b :block/parent ?par]
       [?par :block/marker]
     )
 ]
 :group-by-page? false
 :breadcrumb-show? false
}
#+END_QUERY
4 Likes

I have a query that looks for all the blocks in the journal, that are not a task, and have a reference to the page where the query is, I want to mark some of the blocks with a tag to hide them from the query (because they become useless to my reports). I have tried a lot of things, but I can’t make the query to filter any block tagged with the #hd … can you give me a hint?

#+BEGIN_QUERY
{ :title [:h3 “Notas Adicionales” ]
:query [:find (pull ?b [*])
:in $ ?page
:where
[?x :block/name ?page]
(or-join [?b ?x]
[?b :block/refs ?x]
(and [?x :block/alias ?page] [?b :block/refs ?x])
)
(not [?b :block/marker _])
(not [?child :block/marker _] [?b :block/children ?child])
(not [?b :block/page ?p2] [?p2 :page/journal? false])
]

:group-by-page? true
:result-transform :sort-by-priority
:table-view? false
:breadcrumb-show? false
:inputs [:query-page]
}
#+END_QUERY

Hey, I’ve just started using logseq and I’m already hooked. This query is exactly what I was missing on the journal page. Only I would like the list to be sorted by scheduled date in descending order and limited to 7d in the future. How can I do that? Thanks for your help.

:block/children is not a valid attribute.
You need to go the other way with :block/parent.
Also how you checked the alias was incorrect.

I’ve rewritten the query for you. Let me know if this gives what you are looking for!

#+BEGIN_QUERY
{:title [:h3 "Notas Adicionales"]
 :query [:find (pull ?b [*])
   :in $ ?page
   :where
     [?x :block/name ?page]
     (or-join [?b ?x]
       [?b :block/refs ?x]
       (and 
         [?x :block/alias ?a] 
         [?b :block/refs ?a]
       )
     )
     [?b :block/page ?p] 
     [?p :block/journal? true]
     (not [?b :block/marker _])
     (not 
       [?child :block/parent ?b]
       [?child :block/marker _]
     )
     (not 
        [?b :block/refs ?tag]
        [?tag :block/name "hd"]
     )
 ]
 :group-by-page? true
 :result-transform :sort-by-priority
 :table-view? false
 :breadcrumb-show? false
 :inputs [:query-page]
}
#+END_QUERY
2 Likes

Great to hear you enjoy it :slight_smile:
Two questions.

  1. Which way is descending? Oldest to newest or newest to oldest
  2. What way do you want to limit the tasks? Based on what journal page they are on? Or based on their scheduled date?
1 Like

Hi,

  1. descending means newest first (or next tasks first)
  2. Limit 7d means, only tasks in the next 7d.

Background: I have some yearly tasks. I want to see, which of them coming up in next 7d, but i don’t want to see tasks scheduled in 5 months

If I understand you correctly, this should work for you.
Let me know if any changes are required.

#+BEGIN_QUERY
{:title "List of Tasks"
 :query [:find (pull ?b [*])
   :in $ ?day
   :where
     [?b :block/marker ?m]
     (not [(contains? #{"DONE" "CANCELED"} ?m)] )
     (not 
       [?b :block/parent ?par]
       [?par :block/marker]
     )
     (or-join [?b ?day]
       (not [?b :block/scheduled] )
       (and 
         [?b :block/scheduled ?d]
         [(<= ?d ?day)]
      )
     )
 ]
 :result-transform (fn [result] (sort-by (fn [r] (get-in r [:block/scheduled])) result ) )
 :group-by-page? false
 :breadcrumb-show? false
 :inputs [:+7d]
}
#+END_QUERY
2 Likes

great thank you. one question: is there a documentation for the syntax?

Thank you very much @Siferiax !! Your post give a lot of value to my Logseq. Appreciate it a lot.

Also, I can read the query better now thay you have rephrase it and clean it.

If you don’t mind, I have an additional question. Is it possible to validate that the child blocks don’t have this tag neither? Do I need to nest the “not” conditions?

Thanks as always.

1 Like

Change this,

(not 
       [?child :block/parent ?b]
       [?child :block/marker _]
)

To this,

(not 
       [?child :block/parent ?b]
       [?child :block/marker _]
       [?child :block/refs ?tag]
       [?tag :block/name "hd"]
)

All of those things should be not true.

1 Like

Start here: https://docs.logseq.com/#/page/advanced%20queries

1 Like

thank you very much once again!

But unfortunately, it is not working… :frowning:
if i add the tag to parent block, it dissapears from the query results with all its children blocks, which is ok; but if I remove the tag from the parent, it is shown in the query results with all the child bullets regardless if the child bullets have the “#hd” tag or not…

You probably see the child bullets as part of the parent block.
They are pulled in, because you see the entirety of the block. That is to say the parent with all children etc.
Say your data is:

  • parent
    • child #hd

Then the query will pull parent as part of the result set.
This is then displayed as.

  • parent
    • child #hd

Because it is a direct relationship.

Actually now that I think on it.
What you could try is make it a seperate (not )
Though I feel it would stop the parent from being pulled entirely.

But I’m just doing this off the top of my head!
Just know that child blocks are pulled as part of the parent result. I think that is what is happening anyway.

1 Like

It makes sense. Thanks for answering.

This helped me write my first advanced query. Given a person’s page, it shows all the tasks related to that person. I used query-page so it could go on the right sidebar.

#+BEGIN_QUERY
{ :title [:h3 "👂🏻 Things to discuss"]
  :query [
    :find (pull ?b [*])
    :in $ ?here
    :where
      (page-ref ?b ?here)
      (not
        (task ?b #{"DONE" "CANCELED"}))
      (or
        (property ?b :discuss-with ?here)
        (property ?b :waiting-for ?here)
        (property ?b :follow-up ?here))]
  :inputs [:query-page]
}
#+END_QUERY
4 Likes

Hello.

Looking for a query equivalent to {{query (page-property type [[Thing]])}}, though adding a column with respective task counts…
Not being able to make it work because when filtering blocks through markers, the ones that have NO tasks don’t show up.

Help please – it would go great with Different ways to structure data - #63 by and_yet_it_moves .

I have pages for Project with properties.
Page properties have type=Project and status=Ongoing for Current Projects.

type:: [[Project]]
status:: Ongoing

This advanced query filters them.

#+BEGIN_QUERY
{:title "Project Pages with Status Ongoing"
 :query (and (page-property :status "Ongoing") (page-property :type "Project"))
}
#+END_QUERY

Another Variant

#+BEGIN_QUERY
{:title [:h2 "Project Pages with Status Ongoing"]
 :query [:find (pull ?p [*])
         :where
         (property ?p :type "Project")
         (property ?p :status "Ongoing")
]}
#+END_QUERY

Just shared query incase its useful for others.

2 Likes

I wanted to create dashboard for Tasks Pending by count. Although i was able to use Count to get Count by Types but they were not coming in Table Format.

This query groups task by marker and prints count in table format. Can be modified further for your usecase.

#+BEGIN_QUERY
{:title "✅ Count of DOING and LATER Tasks"
 :query [:find ?marker (count ?b)
         :keys type number
         :where
         [?b :block/marker ?marker]
         [(contains? #{"DOING" "LATER"} ?marker)]
         :group-by ?marker]
 :view (fn [rows] [:table
   [:thead [:tr [:th "Type"] [:th "Count"] ] ]
   [:tbody (for [r rows] [:tr
             [:td (get-in r [:type])]
             [:td (get-in r [:number])]
           ])
   ]])
}
#+END_QUERY

Result

3 Likes

Yeah bit of an issue with that. I’ve been trying to do the same, worked on it awhile back.
We are trying to ask the question.
From all blocks on a page, what are the tasks? Then only count those. There is no way to say, but hold up if you don’t have tasks, count 0.
It’s because we get the dataset first and then count. Instead of for example Excel does a count on the data directly.
So because we filter this data, we cannot have a contradiction.
Give me when x is true and when x is not true, would just return everything and you will end up counting all blocks.
Writing this, I’m thinking lol, maybe with a get :thinking: I’ll have to explore that train of thought later.

1 Like

Hi all, this is awesome thanks! I’ve been doing some research on datalog and logseq quries, but I haven’t been able to include restrictions on time when the query is executed. I would like to see, for instance, my home tasks everyday at 4pm only, and not before that. Is it possible to build a query that excutes its task only if the time is past 4pm, and before 6am ?

Many thanks,
JEEP