Solved

total number of records from another table

Posted on 2014-01-07
6
251 Views
Last Modified: 2014-01-07
hi experts, i hope i get solution
i have 2 table with one to many relation ship,

Courses                      Employees
------------                   ---------------
ID                               CourseID
Name                         EmployeeName
-------------------------------------------------

Course                       Employees
--------------------        ----------------------------------------
ID          Name          CourseID         EmployeeName
--------------------        ----------------------------------------
1           course1            1                 emp1
2           course2            1                 emp2
                                      2                 emp3
                                      2                 emp4
                                      2                 emp5
-------------------------------------------------------------------

i need to write an sql statement that will return
course ID, course Name, Total number of employee in each course

result:
ID         Name          Emp_Total_No.
---------------------------------------------
1           course1           2
2           course2           3
---------------------------------------------

is there any solution to write it in a single query

looking forward for your suggestions

thank you
0
Comment
Question by:AZZA-KHAMEES
6 Comments
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 500 total points
ID: 39761822
Simplest way is a nested query. Haven't tested it but you get the idea.

select ID,
       Name,
       (select count(e.EmployeeName)
          from Emp_Total_No e
         where c.id = e.CourseID
         group by e.EmployeeName) as Emp_Total_No
  from courses c

Open in new window

0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 39761843
Or you could use a join.

SELECT A.ID
	,A.NAME
	,Count(B.CourseID) AS Emp_Total_No
FROM courses A
JOIN Employees B ON A.ID = B.CourseID
GROUP BY B.courseID
	,A.ID
	,A.NAME

Open in new window

0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 39761845
Subquery:
SELECT  ID ,
        Name ,
        ( SELECT    COUNT(CourseID)
          FROM      Employees
          WHERE     Employees.CourseID = Courses.ID
        ) Emp_Total_No
FROM    Courses

Open in new window

left join:
SELECT  C.ID ,
        C.Name ,
        COUNT(E.CourseID) Emp_Total_No
FROM    dbo.Courses C
        LEFT JOIN dbo.Employees E ON C.ID = E.CourseID
GROUP BY C.ID ,
        C.Name

Open in new window


PS.
 Lee Savidge's solution: no need to group by e.EmployeeName in subquery
jeffld's solution: no need to group BY B.courseID
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 12

Expert Comment

by:Jeff Darling
ID: 39761859
Thanks Pourfard.  You are correct.

SELECT A.ID
	,A.NAME
	,Count(B.CourseID) AS Emp_Total_No
FROM courses A
JOIN Employees B ON A.ID = B.CourseID
GROUP BY A.ID
	,A.NAME

Open in new window

0
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39761904
SELECT ID, NAME, Count(CourseID) as Emp_Total_No.
FROM courses,Employees where ID = CourseID GROUP BY ID, NAME
0
 

Author Closing Comment

by:AZZA-KHAMEES
ID: 39761934
thanks
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question