TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Seldomly Used SQL: The Bool Aggregate Functions

45 pointsby neovintageabout 9 years ago

7 comments

nickpetersonabout 9 years ago
I don&#x27;t know if postgres allows one to treat boolean as 0 and 1 natively but in t-sql, the equivalent would be something like:<p><pre><code> DECLARE @orders TABLE ( category VARCHAR(5), express_delivered INT ); INSERT INTO @orders (category, express_delivered) VALUES ( &#x27;food&#x27;, 1); INSERT INTO @orders (category, express_delivered) VALUES ( &#x27;food&#x27;, 0); INSERT INTO @orders (category, express_delivered) VALUES ( &#x27;shoes&#x27;,0); INSERT INTO @orders (category, express_delivered) VALUES ( &#x27;shoes&#x27;,0); INSERT INTO @orders (category, express_delivered) VALUES ( &#x27;auto&#x27;, 1); INSERT INTO @orders (category, express_delivered) VALUES ( &#x27;auto&#x27;, 1); INSERT INTO @orders (category, express_delivered) VALUES ( &#x27;book&#x27;, 1); INSERT INTO @orders (category, express_delivered) VALUES ( &#x27;book&#x27;, 0); SELECT category, CASE WHEN SUM(express_delivered) = 0 THEN 0 ELSE 1 END AS ever_been_express_delivered FROM @orders GROUP BY category</code></pre>
评论 #11641401 未加载
评论 #11639316 未加载
评论 #11638627 未加载
评论 #11638262 未加载
netcraftabout 9 years ago
Very cool. It looks like you can also use `every()` instead of `bool_and()` - and also note that you can provide any boolean value to these, so you can do something like `every(column = &#x27;value&#x27;)...`
rhinoceraptorabout 9 years ago
I love `json_build_object()`.<p>With `json_build_object()`, you can select fields as a JSON object and not have to do that JSON building in your app.<p>Here&#x27;s a snippet demonstrating building, and then querying a JSON object:<p><pre><code> with location as (select json_build_object( &#x27;street1&#x27;, street1, &#x27;street2&#x27;, street2, &#x27;city&#x27;, city, &#x27;state&#x27;, state, &#x27;zip&#x27;, zip ) as loc from my_table) select loc -&gt;&gt; &#x27;street1&#x27; as street1, loc -&gt;&gt; &#x27;street2&#x27; as street2, loc -&gt;&gt; &#x27;city&#x27; as city, loc -&gt;&gt; &#x27;state&#x27; as state, loc -&gt;&gt; &#x27;zip&#x27; as zip from location;</code></pre>
评论 #11637836 未加载
xxbondsxxabout 9 years ago
Doesn&#x27;t this become less valueable as the data grows? You&#x27;ll essentially always have at least one &quot;true&quot; value, and at that point you&#x27;re basically doing the query of:<p>SELECT cateogry FROM orders WHERE express_delivery=TRUE GROUP BY category<p>Also are these aggregate functions as efficient?
评论 #11637681 未加载
评论 #11643288 未加载
cjauvinabout 9 years ago
It&#x27;s actually the first time I&#x27;m seeing the `group by 1, 2` syntax.. is that Postgres-specific?
评论 #11639347 未加载
评论 #11638946 未加载
评论 #11638895 未加载
jnpatelabout 9 years ago
Neat! I use the Python equivalents, `any()` and `all()` quite frequently.
kristianpabout 9 years ago
The title should be &quot;seldomly used Postgres-specific SQL&quot;.