我正在尝试构造正确的sql语句(Oracle)以获取每个customer_id的device_id的计数大于给定值.例如,我想知道拥有3个以上device_ids的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
为了获得超过3台设备的客户:
select customer_id,count(device_id) from YourTable Group by customer_id having count(device_id) > 3