我有以下数据:
customerid addressid
939 936
939 35485
1915 2224
1915 34236
6005 7012
6005 27914
由此查询给出:
select customerid ,addressid
from addresses
where .....
我希望查询每个客户只返回一个地址,我不关心它将是哪个地址。 所以desiered输出可以是:
addressid
936
2224
7012
或
addressid
936
34236
7012
等..
我试着这样做:
select DISTINCT customerid ,addressid
from addresses
where .....
但它没有做任何事......我想到了GROUP BY customerid
,但后来我失去了addressid
。我怎么能这样做?
答案 0 :(得分:2)
按customerid
分组,因为它可以是任何地址,所以使用像max()
这样的aggegation函数来获取最高的
select customerid, max(addressid)
from addresses
group by customerid
答案 1 :(得分:2)
如果你不关心它是哪个地址,这是一个简单的group by
:
select customerid, max(addressid) as addressid
from addresses
group by customerid