Solved

total number of records from another table

Posted on 2014-01-07
6
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 13

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 13

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

734 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