I am having an issue getting the results I want from my SQL statement. I know I’m probably missing something simple but I just can’t see it.
Here are my tables.
Table: Users (RoleID is linked to ID in Roles Table) ID, FirstName, LastName, RoleID 1, Matt, Ryan, 1 2, Chipper, Jones, 1 3, Julio, Jones, 2 4, Jason, Bourn, 3 Table: Roles ID, Name 1, Field Rep 2, Tech 3, Admin Table: FRrequests (UserID is linked to ID in Users table) ID, UserID, Status 1, 1, Open 2, 1, Submitted 3, 1, Delayed 4, 1, Complete
What I want is an SQL statement that shows me a count of all the “Submitted” & “Delayed” requests for all the Field Reps. Below is an example of desired results.
Name Count Chipper Jones 0 Matt Ryan 2
Here is the statement I have so far and the results it gives me.
SELECT Users.FirstName + ' ' + Users.LastName AS Name, COUNT(FRrequests.ID) AS 'Open Requests'
FROM Users INNER JOIN
Roles ON Users.RoleID = Roles.ID LEFT OUTER JOIN
FRrequests ON Users.ID = FRrequests.UserID
WHERE (Roles.Name = N'Field Rep') AND (FRrequests.Status = 'Submitted' OR FRrequests.Status = 'Delayed')
GROUP BY Users.FirstName, Users.LastName
Name Count
Matt Ryan 2
I know that the “AND (FRrequests.Status = ‘Submitted’ OR FRrequests.Status = ‘Delayed’)” part is what is breaking it. If I run it without that in the statement I get all the users but it counts all status not just submitted and delayed. I just can’t figure out what I’m missing to get this to work. Any help would be greatly appreciated.
You are really close, try the following: