Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with.
    
        | 
                
                    | 
                            
                                | Author | Topic |  
                                    | nino59Starting Member
 
 
                                        3 Posts | 
                                            
                                            |  Posted - 2013-05-14 : 10:33:41 
 |  
                                            | Hi,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_text45          flat        236         flat for rent46          house       237          house for rentThe estate agents are stored in table_ea:id        name         username      email                password236        Robert       Royalestate   royalea@gmail.com    x564d5s6s237        David        Boboestate   boboea@gmail.com      x5s4yhhs6sI would like to get a list of estate agents with the quantity of properties they haveusername      qtyRoyalestate    18Boboestate     26etc...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?Regards |  |  
                                    | jackvMaster Smack Fu Yak Hacker
 
 
                                    2179 Posts | 
                                        
                                          |  Posted - 2013-05-14 : 10:35:12 
 |  
                                          | 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--------------------http://www.sqlserver-dba.com |  
                                          |  |  |  
                                    | nino59Starting Member
 
 
                                    3 Posts | 
                                        
                                          |  Posted - 2013-05-14 : 10:48:14 
 |  
                                          | Thanks for this quick answer.The relation is easy.table_ads is connected to table_ea by userid                      idps:sorry but for the form, the forum remove spaces. |  
                                          |  |  |  
                                    | James KMaster Smack Fu Yak Hacker
 
 
                                    3873 Posts | 
                                        
                                          |  Posted - 2013-05-14 : 12:41:41 
 |  
                                          | [code]SELECT  ea.name ,        COUNT(DISTINCT ad.id) AS PropertiesFROM    table_ea ea        LEFT JOIN table_ads ad ON ad.userid = ea.idGROUP BY ea.name;[/code] |  
                                          |  |  |  
                                |  |  |  |