Advanced query for journals from a specific month that have a page-property defined

I found a useful article with a query for journal entries from previous years on the same date (link). I modified it to show me all the journal pages from a specified month. I then modified it to return only the values of a specified page property from those journal pages.

  #+BEGIN_QUERY
  {:title "Month Attendance"
  ; Pull the pages rather than the blocks. We want to show journal pages that have the attendance property defined
  :query [:find (pull ?p [*])
  ; Filter on today, so get that information as a variable to the query
  :in $ ?yyyymm
  :where
      ; First, restrict the results to only journal pages
      [?b :block/page ?p]
      [?p :page/journal? true]
  
      ; Load the journal page properties into ?properties
      [?p :block/properties ?properties]
  
      ; Get the attendance property
      (has-page-property ?p :attendance)
  
      ; Fetch the day of the journal page and store it in ?jd
      [?p :page/journal-day ?jd]
      
      ; Turn the journal date into YYYYMMDD string and store it in ?jds
      [(str ?jd) ?jds]
      
      ; Use subs to cut off the DD and store the YYYYMM in ?journalYYYYMM
      [(subs ?jds 0 6) ?journalYYYYMM]
  
      ; Keep only those journal pages whose YYYYMM are equal to the specified argument
      [(= ?journalYYYYMM ?yyyymm)]
    ]
  ; Make logseq provide today's date. Use double quotes rather than single so things work.
  :inputs ["202308"]
  }
  #+END_QUERY

This mostly does what I want. There are some improvements I would like to make, but I’m not sure how.

  1. I would like to be able to insert this into a page multiple times as a method call rather than copying and pasting the entire query. I would like to have an Attendance page that showed results for each month separately.
  2. I would like to convert the result table into something with a calendar view. I have tried to do this by modifying the Block Calendar plugin, but found that the resulting display didn’t persist when I exported public pages to HTML.
1 Like

Thanks a lot for sharing this query. This help me made by query to Tasks By Month Query.

#+BEGIN_QUERY
{:title [:h3 "🧭️ Tasks By Month"]
 :query [:find ?dYYYYMM (count ?b)
       :keys type number
       :in $ ?start ?end
       :where   
       (task ?b #{"LATER" "TODO" "DOING"})
       (or [?b :block/scheduled ?d] [?b :block/deadline ?d])
       [(str ?d) ?ds]
       [(subs ?ds 0 6) ?dYYYYMM]
       [(> ?d ?start)]
       [(< ?d ?end)]
       ]
 :inputs [:-1w :+6m]
:collapsed? false
 :group-by ?d
 :view (fn [rows]
       [:table
        [:thead [:tr [:th "Month"] [:th "Count"]]]
        [:tbody
         (let [sorted-rows (sort-by :type rows)]
           (for [r sorted-rows]
             [:tr
              [:td (get-in r [:type])]
              [:td (get-in r [:number])]]))]])
}
#+END_QUERY