Fork me on GitHub
#honeysql
<
2023-08-17
>
markbastian23:08:59

How might I do an expression such as this in honesql (request_end and request_begin are columns):

avg((request_end - request_begin) / 1000.0) as avg_exec_time

seancorfield00:08:00

[:- :request_end :request_begin] for the subtraction... [:/ ... 1000.0] for the divide... [:avg ...] for the function call... [... :avg_exec_time] for the alias

seancorfield00:08:27

And then I assume you want to use it in a SELECT? {:select [ expr expr expr ... ] ...} so putting that all together you get:

{:select [[[:avg [:/ [:- :request_end :request_begin] 1000.0]] :avg_exec_time] ...] ...}
depending on whether you need to select a second or subsequent expression...

seancorfield00:08:46

If the documentation can make this clearer, let me know.

markbastian00:08:45

Cool! I’ll give that a shot. I think the main thing is just knowing where to look. Thanks!

seancorfield00:08:28

The Getting Started page has these examples:

markbastian00:08:10

Ah, I thought I was doing the above but it’s the nested brackets that got me originally. I was doing:

(hsql/format
  {:select [[:- :request_end :request_begin]]
   :from   [:foo]})
but need to do:
(hsql/format
  {:select [[[:- :request_end :request_begin] :diff]]
   :from   [:foo]})
Once I got it, it makes sense that you need to add the extra nesting to capture the expression and the alias. If I get some time maybe I’ll put together a doc PR.

2
markbastian00:08:18

Thanks for the clarification!