假设有一张表
_ _
a 1
a 2
b 2
c 3
c 4
c 1
d 2
e 5
e 6
如何选择每组所有行的不同最小值?
所以这里的预期结果是:
_ _
a 1
b 2
c 1
d 2
e 5
EDIT
我的实际表包含更多列,我想全部选择它们。这些行仅在最后一列(示例中的第二列)有所不同。我是 SQL 新手,可能我的问题在最初的视图中格式不正确。
实际的架构是:
| day | currency ('EUR', 'USD') | diff (integer) | id (foreign key) |
是重复对(day, currency)
其不同之处在于(diff, id)
。我想查看包含唯一对的表格(day, currency)
具有最低限度diff
从原始表。
Thanks!
在你的情况下,它就像这样简单:
select column1, min(column2) as column2
from table
group by column1
对于两列以上,我可以建议:
select top 1 with ties
t.column1, t.column2, t.column3
from table as t
order by row_number() over (partition by t.column1 order by t.column2)
看看这个帖子https://stackoverflow.com/a/13652861/1744834
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)