8

我正在尝试构建正确的 sql 语句 (Oracle) 以获取大于给定值的每个 customer_id 的 device_id 计数。例如,我想知道具有 3 个以上 device_id 的 customer_id。一个 device_id 只能关联一个 customer_id,而一个 customer_id 可能有多个 device_id。

Table:
device_id
customer_id
....

Data (device_id, customer_id):
00001, CUST1
00002, CUST1
00003, CUST1
00004, CUST1
00005, CUST2
00006, CUST2
4

3 回答 3

18

获取拥有 3 台以上设备的客户:

select customer_id, count(device_id)
from YourTable
Group by customer_id
having count(device_id) > 3
于 2012-08-15T17:31:05.490 回答
3

您将需要使用GROUP BYandHAVING子句

select customer_id, count(device_id)
from YourTable
group by customer_id
having count(device_id) > 3
于 2012-08-15T17:32:17.430 回答
1

你需要的是有子句:

select customer_id, count(*)
from t
group by customer_id
having count(*) > 3
于 2012-08-15T17:32:20.550 回答