Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have two tables, customers and numbers.

numbers contains a 'customer' field which corresponds to customers.id; the customer who owns the number.

I would like to return a list of customers who have at least one number, and how many numbers they own.

I've tried the following query but it's giving an error:

SQL
select c.name as "Customer", count(*) as "Numbers" from customers c, numbers n where n.customer=c.id and (select count(*) from numbers where customer=c.id) > 0


What I have tried:

---------------------
In question
---------------------
Posted
Updated 10-Nov-17 1:42am
v2

SELECT MAX(c.name) AS "Customer",COUNT(n.customer) as "Numbers" 
   FROM 
  customers c INNER JOIN numbers n 
     on(n.customer=c.id) 
   GROUP BY c.id
     HAVING count(n.customer)>0
---------------------------------------
SELECT c.name  AS "Customer",Numbers 
   FROM (SELECT ID,Name from customers)AS c
   INNER JOIN (
  SELECT customer,Counr(1) AS "Numbers" from numbers GROUP BY customer)n 
     ON(n.customer=c.id) 
         WHERE Numbers<>0 
 
Share this answer
 
v2
Comments
[no name] 10-Nov-17 7:51am    
Perfect, that works! Thank you c:
Santosh kumar Pithani 10-Nov-17 7:52am    
Welcome AntiRix
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900