我有下表:
MobileNumber LocID
a 2
b 3
a 2
b 3
b 4
c 5
etc
手机号码可以重复并具有不同的LocID。我想拥有仅存在于一个LocID中的移动设备列表(在本例中为“a”和“c”)。我尝试了以下但没有任何成功。有任何帮助吗?
select MobileNumber, count(*)
from table1
where timestamp > '2012-03-01 00:00'
group by MobileNumber, LocID
having count(*) = 1;
答案 0 :(得分:4)
尝试:
select MobileNumber, count(*)
from table1
where timestamp > '2012-03-01 00:00'
group by MobileNumber
having count(distinct LocID) = 1;