TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The many faces of DISTINCT in Postgres (2017)

187 点作者 rsecora将近 2 年前

11 条评论

codeflo将近 2 年前
&gt; A classic job interview question is finding the employee with the highest salary in each department.<p>Here’s a cheat code, in case you need to write a database query and don’t remember all the fancy join tricks: Problems like this often have a very straightforward solution with subselects. In this case, the main select gets the departments, and a subselect (with limit 1) fetches the top employee for each department.<p>That’s a very natural, compositional way of thinking. Granted, it’s not the most optimized way to do it, but more often then not, the resulting query plan is perfectly fine.
评论 #36029559 未加载
评论 #36034729 未加载
评论 #36030152 未加载
评论 #36043875 未加载
评论 #36041134 未加载
评论 #36030119 未加载
neallindsay将近 2 年前
The author mentions having to get over the lack of upsert when moving from Oracle. But readers might like to know this isn’t a problem anymore since Postgres got “INSERT … ON CONFLICT UPDATE …”.
评论 #36031790 未加载
评论 #36030809 未加载
评论 #36030641 未加载
评论 #36030473 未加载
zzzeek将近 2 年前
I&#x27;ve never gotten into DISTINCT ON and it&#x27;s always confused me, I&#x27;d rather see the query with MAX and GROUP BY if I&#x27;m looking for &quot;the highest X in groups of Y&quot;. For the same reason I don&#x27;t prefer RSA-in-three-lines.
ak39将近 2 年前
I’ve never truly used DISTINCT and felt comfortable for using it. Always felt using it revealed a design smell in my query. (Been doing SQL for 30 years and still!)
评论 #36116421 未加载
cpursley将近 2 年前
I only learned about the ranked query approach last week thanks to ChatGTP. Helped me solve a hairy query that rolled up activity events grouped by time periods. Before that I was struggling with distinct (and it was slow).<p>I’ve avoided ChatGTP until recently and at least for SQL refactoring, it’s great. The interesting part is the ranked example ChatCTP gave me was almost identical to the one in this post. I wonder if they’re (ChatGTP) is training up on technical blog posts.
评论 #36031012 未加载
maweki将近 2 年前
&quot;(easy CS students, I know it&#x27;s not normalized…)&quot;<p>Sure it is. As long as you&#x27;re not storing any other information on the department in the employee table.
评论 #36030642 未加载
评论 #36056945 未加载
pophenat将近 2 年前
Microsoft SQL Server now also has IS [NOT] DISTINCT FROM. <a href="https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;sql&#x2F;t-sql&#x2F;queries&#x2F;is-distinct-from-transact-sql?view=sql-server-ver16" rel="nofollow">https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;sql&#x2F;t-sql&#x2F;queries&#x2F;is-disti...</a>
xupybd将近 2 年前
MSSQL does have something closer to array agg.<p><a href="https:&#x2F;&#x2F;www.mssqltips.com&#x2F;sqlservertip&#x2F;5542&#x2F;using-for-xml-path-and-stringagg-to-denormalize-sql-server-data&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.mssqltips.com&#x2F;sqlservertip&#x2F;5542&#x2F;using-for-xml-pa...</a>
评论 #36031753 未加载
t00将近 2 年前
&gt; We can immediately see that everyone in the support department are making the same salary.<p>Bonnie Robertson is a thriving 10X support high earner
justinclift将近 2 年前
2017
评论 #36030837 未加载
评论 #36031802 未加载
kubota将近 2 年前
Oracle listaggs do support distinct, since 19c.