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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Count list from two tables

Author  Topic 

leearenaa
Starting Member

3 Posts

Posted - 2013-06-20 : 00:32:42
Hi there,
How can I query two tables to get the list and number of total count? Below is my tables:
User's Table:
UserID | ProfessionID
1234 | 1
3241 | 2
3435 | 1

Profession's table
ProfessionID | ProfessionName
1 | Student
2 | Chef
3 | Others

So how can I query that the output will be coming as below?
ProfessionName | TotalCount
Student | 2
Chef | 1
Others | 0

Thank you for the advance.

LeeAreNaa

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-20 : 00:50:14
[code]
SELECT p.ProfessionName,
COUNT(UserID) AS TotalCount
FROM Profession p
LEFT JOIN User u
ON p.ProfessionID = u.ProfessionID
GROUP BY p.ProfessionName
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

leearenaa
Starting Member

3 Posts

Posted - 2013-06-20 : 01:55:37
Its look great...
Thank you..

quote:
Originally posted by visakh16


SELECT p.ProfessionName,
COUNT(UserID) AS TotalCount
FROM Profession p
LEFT JOIN User u
ON p.ProfessionID = u.ProfessionID
GROUP BY p.ProfessionName


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs




LeeAreNaa
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-20 : 02:00:56
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -