SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 SQL Server Administration (2012)
 Counter sql
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

3 Posts

Posted - 05/14/2013 :  10:33:41  Show Profile  Reply with Quote

My boss ask me some information: How many properties has each estate agent in our database. I need your help please.

I have a database with estate agents and properties.
The properties are stored in table_ads:
id category userid ad_text
45 flat 236 flat for rent
46 house 237 house for rent
The estate agents are stored in table_ea:
id name username email password
236 Robert Royalestate royalea@gmail.com x564d5s6s
237 David Boboestate boboea@gmail.com x5s4yhhs6s

I would like to get a list of estate agents with the quantity of properties they have

username qty
Royalestate 18
Boboestate 26

So I need to creqate an SQL query which will count the quantity of each estate agent, display this quantity with the username.

Can you help me please?


Flowing Fount of Yak Knowledge

United Kingdom
2169 Posts

Posted - 05/14/2013 :  10:35:12  Show Profile  Visit jackv's Homepage  Reply with Quote
Could you supply the DDL of the tables - so we can understand the relationships of the tables, or is there just one table?

Jack Vamvas
Go to Top of Page

Starting Member

3 Posts

Posted - 05/14/2013 :  10:48:14  Show Profile  Reply with Quote
Thanks for this quick answer.

The relation is easy.

table_ads is connected to table_ea by
userid id

ps:sorry but for the form, the forum remove spaces.
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3867 Posts

Posted - 05/14/2013 :  12:41:41  Show Profile  Reply with Quote
SELECT  ea.name ,
        COUNT(DISTINCT ad.id) AS Properties
FROM    table_ea ea
        LEFT JOIN table_ads ad ON ad.userid = ea.id
GROUP BY ea.name;
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000