Select query w/ group by (1 Viewer)

jsdba

Registered User.
Local time
Today, 15:22
Joined
Jun 25, 2014
Messages
165
Hi Experts,

I'm trying to write a query that will return only the record that only have RoleID = 1. Group BY ProjectNum. E.g. the record that should be returned is 18003-101-1 because all other project groupings include records with RoleIDs different from 1.

Last bit of info to consider, all groups will have at least one record, that record will have RoleID = 1

See attachment
 

Attachments

  • tblProjectTeam.PNG
    tblProjectTeam.PNG
    4.5 KB · Views: 44

theDBguy

I’m here to help
Staff member
Local time
Today, 12:22
Joined
Oct 29, 2018
Messages
21,491
Hi. You could try something like this:
Code:
SELECT ProjectNum, EmployeeID
FROM tblProjectTeam
WHERE RoleID=1
GROUP BY ProjectNum, EmployeeID
HAVING Count(*)=1
 

plog

Banishment Pending
Local time
Today, 14:22
Joined
May 11, 2011
Messages
11,653
Can RoleID be null or less than 1?
 

plog

Banishment Pending
Local time
Today, 14:22
Joined
May 11, 2011
Messages
11,653
You are going to need a subquery to do this:

Code:
SELECT ProjectNum
FROM tblProjectTeam
GROUP BY ProjectNum
HAVING Max(RoleID)=1;

That will return all the ProjectNum values that meet your criteria. You can then get all the records by taking that query and making a new one with it and tblProjectTeam and joining them via ProjectNum
 

jsdba

Registered User.
Local time
Today, 15:22
Joined
Jun 25, 2014
Messages
165
You are going to need a subquery to do this:

Code:
SELECT ProjectNum
FROM tblProjectTeam
GROUP BY ProjectNum
HAVING Max(RoleID)=1;

That will return all the ProjectNum values that meet your criteria. You can then get all the records by taking that query and making a new one with it and tblProjectTeam and joining them via ProjectNum

Thank you plog. I'm working on it and so far it works with the testing table. I'll update final results later.
 

Attachments

  • tblProjectTeam.PNG
    tblProjectTeam.PNG
    10.3 KB · Views: 37

Users who are viewing this thread

Top Bottom