This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-02-08
Channels
- # announcements (14)
- # babashka (12)
- # beginners (140)
- # calva (2)
- # cider (22)
- # clj-commons (14)
- # clj-kondo (49)
- # cljdoc (34)
- # clojure (92)
- # clojure-europe (41)
- # clojure-france (2)
- # clojure-new-zealand (2)
- # clojure-nl (2)
- # clojure-norway (60)
- # clojure-uk (17)
- # clojured (2)
- # clojurescript (7)
- # community-development (3)
- # conjure (2)
- # cryogen (13)
- # cursive (4)
- # data-oriented-programming (2)
- # datahike (5)
- # datomic (12)
- # defnpodcast (10)
- # events (2)
- # fulcro (20)
- # gratitude (3)
- # honeysql (4)
- # introduce-yourself (3)
- # jobs (10)
- # lsp (58)
- # malli (12)
- # missionary (19)
- # off-topic (8)
- # pathom (18)
- # podcasts-discuss (1)
- # polylith (41)
- # releases (1)
- # remote-jobs (3)
- # shadow-cljs (52)
- # spacemacs (1)
- # sql (37)
- # xtdb (19)
Hey, I wonder if is there a better way to write a query using CASE WHEN
within the SUM
? For example, getting rid of the :raw
somehow.
{:select [[[:raw "SUM(CASE WHEN direction = 'in' THEN amount ELSE 0 END) as in"]]
[[:raw "SUM(CASE WHEN direction = 'out' THEN amount ELSE 0 END) as out"]]]
:from [:foo]}
@wcalderipe Why would you need :raw
there?
dev=> (sql/format {:select [[[:sum [:case [:= :direction "in"] :amount :else 0]] :in]
#_=> [[:sum [:case [:= :direction "out"] :amount :else 0]] :out]]
#_=> :from :foo})
["SELECT SUM(CASE WHEN direction = ? THEN amount ELSE ? END) AS in, SUM(CASE WHEN direction = ? THEN amount ELSE ? END) AS out FROM foo" "in" 0 "out" 0]
I wasn't sure if I need it; hence the question here. It's my first time using HoneySQL so I'm still learning how to work with it. Thanks for the answer 😄
1
Use of :raw
should be extremely rare -- and only needed for really unusual corner case syntax.
👌 1