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.

The SQL filter clause: selective aggregates

138 pointsby MarkusWinandalmost 9 years ago

8 comments

MichaelBurgealmost 9 years ago
At an old job, me and another person wrote an in-house SQL-like dialect that compiled to access our C++ data structures. It was kind of nice just throwing whatever junk you wanted into your SQL without having to worry about any standards.<p>Want a filter clause? Got it.<p>Need a weighted distinct count? Now it&#x27;s part of the language.<p>Also, you can rephrase a lot of these SQL features as subqueries. It&#x27;s surprising how many database bugs you can find when you do it. Not so much in Postgres, but I probably found a dozen in Redshift. I mean rephrasing this:<p><pre><code> select sum(x) filter (where x &lt; 5), sum(x) filter (where x &lt; 7) from generate_series(1,10,1) s(x) ; </code></pre> as this(where t is either a permanent table, view, or CTE as appropriate):<p><pre><code> with t as ( select * from generate_series(1,10,1) s(x) ) select (select sum(x) from t where x &lt; 5), (select sum(x) from t where x &lt; 7) ; </code></pre> Though for Postgres the filter will likely have better performance.
评论 #11801514 未加载
评论 #11801165 未加载
评论 #11802209 未加载
Xophmeisteralmost 9 years ago
Syntactic sugar that&#x27;s only natively supported in PostgreSQL? The site&#x27;s banner says, &quot;A lot has changed since SQL-92&quot;. Be that as it may, but it seems no one has really bothered catching up. I wonder why that is...<p>My guess is that such extensions, while useful, are somewhat marginalised features in terms of usage. Thus, no one ever learns them formally and just Googles for what they need -- if it comes up -- and get the CASE solution, in this case (pun unintentional). Hence perpetuating that pattern. Also, of course, the CASE solution is a lot more powerful as the returned expression, that gets fed into the aggregate function, can be basically anything.
评论 #11801703 未加载
评论 #11801005 未加载
评论 #11802988 未加载
westurneralmost 9 years ago
You can do these with Ibis and various SQL engines:<p>* <a href="http:&#x2F;&#x2F;docs.ibis-project.org&#x2F;sql.html#aggregates-considering-table-subsets" rel="nofollow">http:&#x2F;&#x2F;docs.ibis-project.org&#x2F;sql.html#aggregates-considering...</a><p>* <a href="https:&#x2F;&#x2F;github.com&#x2F;cloudera&#x2F;ibis&#x2F;tree&#x2F;master&#x2F;ibis&#x2F;sql" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;cloudera&#x2F;ibis&#x2F;tree&#x2F;master&#x2F;ibis&#x2F;sql</a> (PostgreSQL, Presto, Redshift, SQLite, Vertical)<p>* <a href="https:&#x2F;&#x2F;github.com&#x2F;cloudera&#x2F;ibis&#x2F;blob&#x2F;master&#x2F;ibis&#x2F;sql&#x2F;alchemy.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;cloudera&#x2F;ibis&#x2F;blob&#x2F;master&#x2F;ibis&#x2F;sql&#x2F;alchem...</a> (SQLAlchemy)
codegeekalmost 9 years ago
&quot;with&quot; is not supported by mysql but sqlite supports it ? wow, didn&#x27;t know that.
评论 #11801147 未加载
rubyfanalmost 9 years ago
Is this all that different from Hive&#x27;s analytic functions? I don&#x27;t think I can put a WHERE filter right in the OVER clause though.<p><i>SELECT a, COUNT(b) OVER (PARTITION BY c) FROM T;</i><p><a href="https:&#x2F;&#x2F;cwiki.apache.org&#x2F;confluence&#x2F;display&#x2F;Hive&#x2F;LanguageManual+WindowingAndAnalytics" rel="nofollow">https:&#x2F;&#x2F;cwiki.apache.org&#x2F;confluence&#x2F;display&#x2F;Hive&#x2F;LanguageMan...</a>
ibejoebalmost 9 years ago
For those on Oracle or MS SQL, be aware of the PIVOT and UNPIVOT features. It&#x27;s not as general as this, but it&#x27;s terse and works well for the case, i.e., running an aggregate across specific buckets.
ams6110almost 9 years ago
If I&#x27;m understanding this correctly, you can also do this sort of thing as a UNION of aggregate queries, each one with its own WHERE clause corresponding to the desired FILTER.
d33almost 9 years ago
It would be nice to see an example for this kind of query.<p>BTW, SQL seems like a terrible language. For example, why would it need to enforce ordering for keywords like ORDER BY, WHERE, GROUP BY? So many times I had made a mistake that boiled down to putting them in the correct order...
评论 #11801297 未加载
评论 #11802146 未加载
评论 #11801366 未加载
评论 #11808920 未加载
评论 #11804209 未加载