Aggregation of results

Hi!

I have started using Logseq for all things, but there is one thing keeping me from leaving Onenote 100% and that is my personal time tracking system.
I work on many different projects, and was thinking of using properties in Logseq for this, like:

project:: ProjectName
hours:: NumberOfHoursThatDay

Then I want to have a query that sums up all the hours for each project every week.

To start off easily I thought I could sum up all hours for one project.
Getting the hours is easy enough:

:query [
    :find (pull ?b [*])
    :where
    (property ?b :project "MyFavouriteProject")
 ]

But then, how to sum up these hours? I have tried a lot of different things, but without success.
I am not that familiar with Clojure, but normally I would just do a reduce with a (+) function to achieve something like this. But where to put it? In the result-transform section or the view section? These features are not very well documented, so I don’t know. All my attempts at using result-transform have failed. I even struggle extracting the hours part of the result.

Any help pushing me in the right direction is welcomed!
Thanks!

Welcome. Have you tried the approaches in threads like the following ones?

Indeed I have. I think I have been through pretty much all the threads that contain any information about similar topics.

@nilsml You can use (:PROP-NAME item) to get the property value from certain block:
2024-03-27 16.15.40

Both ways work:

  • Use a function macro:
    • Go to the block which has your query that works.
    • Under it, insert a nested/child block.
    • For its content, enter this: Total hours: {{function (sum :hours)}}
  • Augment your query directly:
    #+BEGIN_QUERY
    {
      :query [
        :find (sum ?hours)
        :where
        (property ?b :project "MyFavouriteProject")
        [?b :block/properties ?prop]
        [(get ?prop :hours) ?hours]
      ]
    }
    #+END_QUERY
    

Thanks for your example!

I forgot to mention that I got the function macro working, however I would like to do this in one query so that in the end I am able to create a table where I have all projects and sums. Also I want to do this with advanced query to learn.