Solved

total number of records from another table

Posted on 2014-01-07
6
253 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 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

735 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