用于仅选择第一列中具有相同数据的行的第一次出现的 SQL 查询
是否有一个简洁的 SQL 查询可以返回行,以便仅返回第一列中具有相同数据的第一次出现的行?也就是说,如果我有像
blah something
blah somethingelse
foo blah
bar blah
foo hello
查询这样的行,应该给我第一、第三和第四行(因为第一行是第一列中第一次出现“blah”,第三行是“foo”在第一列中第一次出现)第一列,第四行是第一列中第一次出现的“bar”)
。 ,如果这很重要的话,
更新:对于不清楚的表定义感到抱歉,这里更好;“blah”、“foo”等表示该行中第一列的值。
blah [rest of columns of first row]
blah [rest of columns of second row]
foo [-""- third row]
bar [-""- fourth row]
foo [-""- fifth row]
Is there a neat SQL query that would return rows so that only first occurrences of rows, that have same data in the first column, would be returned? That is, if I have rows like
blah something
blah somethingelse
foo blah
bar blah
foo hello
the query should give me the first, third and fourth rows (because first row is the first occurrence of "blah" in the first column", third row is the first occurrence of "foo" in the first column, and fourth row is the first occurrence of "bar" in the first column).
I'm using H2 database engine, if that matters.
Update: sorry about the unclear table definition, here's it better; the "blah", "foo" etc. denote the value of the first column in the row.
blah [rest of columns of first row]
blah [rest of columns of second row]
foo [-""- third row]
bar [-""- fourth row]
foo [-""- fifth row]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的意思是按字母顺序排列第 2 列,则可以使用以下 SQL 来获取这些行:
If you meant alphabetically on column 2, here is some SQL to get those rows:
分析请求可以解决这个问题。
但这只是 V1.3.X 的优先级 2
http://www.h2database.com/html/roadmap.html?highlight=RANK&search=rank#firstFound
Analytic request could do the trick.
But this is only a priority 2 for the V1.3.X
http://www.h2database.com/html/roadmap.html?highlight=RANK&search=rank#firstFound
我认为这符合你的要求,但我不是 100% 确定。 (也基于 MS SQL Server。)
I think this does what you want but I'm not 100% sure. (Based on MS SQL Server too.)
如果您对最快的查询感兴趣:在表的第一列上建立索引相对重要。这样查询处理器就可以扫描该索引中的值。然后,最快的解决方案可能是使用“外部”查询来获取不同的 c1 值,加上“内部”或嵌套查询来获取第二列的可能值之一:
If you are interested in the fastest possible query: It's relatively important to have an index on the first column of the table. That way the query processor can scan the values from that index. Then, the fastest solution is probably to use an 'outer' query to get the distinct c1 values, plus an 'inner' or nested query to get one of the possible values of the second column: