SELECT SQL syntax for counting in a WHERE clause

I am trying to build the correct SQL query (Oracle) to get a device_id counter for each customer_id that is greater than the given value. For example, I want to know a client_id that contains more than 3 Device_ID. A single device_id can have only one client_id associated with it, while a customer_id can have many device_ids.

Table: device_id customer_id .... Data (device_id, customer_id): 00001, CUST1 00002, CUST1 00003, CUST1 00004, CUST1 00005, CUST2 00006, CUST2 
+6
source share
3 answers

To get customers with more than three devices:

 select customer_id, count(device_id) from YourTable Group by customer_id having count(device_id) > 3 
+14
source

Do you want to use the GROUP BY and the HAVING

 select customer_id, count(device_id) from YourTable group by customer_id having count(device_id) > 3 
+3
source

What you need is this sentence:

 select customer_id, count(*) from t group by customer_id having count(*) > 3 
+1
source

Source: https://habr.com/ru/post/922966/


All Articles