SSMS Joins and COUNT function

时间:2017-11-16 11:31:04

标签: sql sql-server tsql

I've got a database which records all the jobs that we complete for the various departments in the business.

Table 1 is Department – this stores the name of the department alongside it's ID

Table 2 is Section – this stores the name of section alongside it's ID (one department could have several sections)

Table 3 is User – name of the person who completed the task, alongside the department and section id that the task was for)

Table 4 is date of completion – this contains the name of the person who completed the task and the date it was submitted to the system

Big boss wants me to provide a report that will detail how many jobs each department and section have submitted within a certain time frame.

So far I have got this:

SELECT  Dept.DepartmenttId,
Name.SectionId,
count (Name.DepartmentId)  
FROM    Name  
LEFT join Job on Name.UserId = Job.UserId  
WHERE Job.DateSubmitted > '2017-09-01 00:00:00.000' and Job.DateSubmitted <'2017-09-30 23:59:59.000'  
Group by Name.DepartmentId, Name.SectionId

That gives me results like:

BOB      2    3  
STEVE    1    2  

where I am getting the name of person alongside the department ID and section ID.

What I want is:

Bob    IT    Development

so I can see the name of the department and section rather than the IDs.

I've been playing around with joins, but everything I try breaks the count part. Is there a way to get the count and the names within one query? Have I gone wrong in the way I've structured what I have so far?

Current tables:

DEPARTMENT

DepartmentId :: DepartmentName  
1 :: IT  
2 :: Marketing  
3 :: Finance  

SECTION

SectionId :: SectionName    
1 :: Development  
2 :: Helpdesk  
3 :: Architecture  
4 :: Procurement

NAME

Name :: JobId :: DepartmentId :: SectionId   
Bob :: J1234 :: 1 :: 2  
Steve :: J1235 :: 3 :: 4

JOB

JOBId :: DateSubmitted  
J1234 :: 02/09/2017  
J1235 :: 04/09/2017

2 个答案:

答案 0 :(得分:1)

请检查以下SQL选择

SELECT  
    Name.Name, DepartmentName, SectionName
FROM Name 
INNER JOIN Job on Name.JobId = Job.JobId
INNER JOIN DEPARTMENT on Name.DepartmentID = DEPARTMENT.DepartmentID
INNER JOIN Section on Name.SectionID = Section.SectionID
WHERE 
    Job.DateSubmitted between '2017-09-01 00:00:00.000' and '2017-09-30 23:59:59.000'  

要显示作业计数,您可以使用以下SQL查询

SELECT  
    DepartmentName, SectionName, Count(*) as JobCount
FROM Job 
INNER JOIN DEPARTMENT on Name.DepartmentID = DEPARTMENT.DepartmentID
INNER JOIN Section on Name.SectionID = Section.SectionID
WHERE 
    Job.DateSubmitted between '2017-09-01 00:00:00.000' and '2017-09-30 23:59:59.000'
Group By
    DepartmentName, SectionName

答案 1 :(得分:0)

根据Eralper提供的查询,我想我已经知道了:

SELECT DepartmentName, SectionName, Count(*) as JobCount  
FROM Name  
INNER JOIN Department on Name.DepartmentId = Department.DepartmentId  
INNER JOIN Section on Name.SectionId = Section.SectionId  
INNER JOIN Job on Name.JobId = Job.JobId  
WHERE Job.DateSubmitted between '2017-09-01 00:00:00.000' and '2017-09-20 23:59:59.000'  
GROUP BY DepartmentName, SectionName