我确定我在这里错过了一些简单的东西。我有一个返回数据集的查询,如下所示:
**OrderNum Address**
12345 1405 W Hollywood
12346 1555 3rd Ave
22334 PO Box 1467
22334 1327 14th Ln S.
我现在想要的是将每个订单中只能看到一个地址的地方计算,或者如果有多个地址,只需显示多个'像这样
**OrderNum Address**
12345 1405 W Hollywood
12346 1555 3rd Ave
22334 Multiple
如果我尝试这样做,例如&count;(CountNum)> 1 ...'然后我必须按地址分组,但它不起作用。其他想法?我的大脑被困在这上面,所以我需要一个轻推。谢谢!
答案 0 :(得分:1)
select ordernum,
case when count(*) > 1
then 'multiple'
else max(address)
end as address
from your_table
group by ordernum
您需要汇总所有未分组的列。我猜你没有使用max(address)
或类似的东西。
答案 1 :(得分:1)
使用窗口功能:
select distinct ordernum,
(case when AddrCount > 1 then 'Multiple' else address end) as address
from (select *, count(*) over (partition by OrderNum) as AddrCount
from table
) t;