From 4c711d790aa59640a593922c068c3d0942e60117 Mon Sep 17 00:00:00 2001 From: Mark Bastian Date: Fri, 18 Aug 2023 10:37:45 -0600 Subject: [PATCH] Adding an example to the README.md file to clarify how custom functions are used in select statements along with their corresponding column alias. --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 93013f2..983ef94 100644 --- a/README.md +++ b/README.md @@ -606,6 +606,20 @@ regular function calls in a select: => ["SELECT MAX(id) FROM foo"] ``` +Custom columns using functions are built with the same vector format. +Be sure to properly nest the vectors so that the first element in the selection +is the custom function and the second is the column alias. +```clojure +(sql/format + {:select [:job_name ;; A bare field selection + [[:avg [:/ [:- :end_time :start_time] 1000.0]] ;; A custom function + :avg_exec_time_seconds ;; The column alias + ]] + :from [:job_data] + :group-by :job_name}) +=> ["SELECT job_name, AVG((end_time - start_time) / ?) AS avg_exec_time_seconds FROM job_data GROUP BY job_name" 1000.0] +``` + If a keyword begins with `'`, the function name is formatted as a SQL entity rather than being converted to uppercase and having hyphens `-` converted to spaces). That means that hyphens `-` will become underscores `_`