?
Solved

total number of records from another table

Posted on 2014-01-07
6
Medium Priority
?
262 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 2000 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

800 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