473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How would I find records with a specific set of related records?

107 Expert 100+
I'm working on an Employee database and would like to find employees who have experience in several categories. There is an employee table and a related employee experience table. For simplicity let's say the employee table has just the field EmployeeID and the EmployeeExperience table has fields EmployeeID and ExperienceCategoryID. I want to find employees with ExperienceCategoryID values 2, 3, 8, and 10. Finding an employee with any one of these categories is easy, but I want to find employees with all of the selected categories. How would I do that?
Oct 14 '14 #1

✓ answered by Rabbit

Assuming that the combination of EmployeeID and ExperienceCategoryID is unique in the employee experience table, filter for those categories you're looking for, do a count grouping by the employee, return only those with a count of 4.

4 877
Rabbit
12,516 Expert Mod 8TB
Assuming that the combination of EmployeeID and ExperienceCategoryID is unique in the employee experience table, filter for those categories you're looking for, do a count grouping by the employee, return only those with a count of 4.
Oct 14 '14 #2
Steven Kogan
107 Expert 100+
In my case the experience is also related to projects and date ranges so they can have the same experience listed several times. I could group by EmployeeID and ExperienceCategoryID, and then create another query doing what you described. Thanks!
Oct 14 '14 #3
jforbes
1,107 Expert 1GB
You could also try something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT Emp.EmployeeID
  2. FROM Employee AS Emp
  3. WHERE Emp.EmployeeID IN (SELECT EmpEx.EmployeeID FROM EmployeeExperience AS EmpEx WHERE EmpEx.ExperienceCategoryID=[Cat1] OR [Cat1] IS NULL)
  4. AND Emp.EmployeeID IN (SELECT EmpEx.EmployeeID FROM EmployeeExperience AS EmpEx WHERE EmpEx.ExperienceCategoryID=[Cat2] OR [Cat2] IS NULL)
  5. AND Emp.EmployeeID IN (SELECT EmpEx.EmployeeID FROM EmployeeExperience AS EmpEx WHERE EmpEx.ExperienceCategoryID=[Cat3] OR [Cat3] IS NULL)
  6. AND Emp.EmployeeID IN (SELECT EmpEx.EmployeeID FROM EmployeeExperience AS EmpEx WHERE EmpEx.ExperienceCategoryID=[Cat4] OR [Cat4] IS NULL)
The ... OR [Cat1] IS NULL... statements allow you to pass null values if you have fewer than 4 Categories that you are trying to match.
Oct 14 '14 #4
NeoPa
32,556 Expert Mod 16PB
Try this idea :
Expand|Select|Wrap|Line Numbers
  1. SELECT   [EmployeeID]
  2. FROM     [EmployeeExperience]
  3. WHERE    ([ExperienceCategoryID] In(2,3,8,10))
  4. GROUP BY [EmployeeID]
  5. Having   (Count(*)=4)
It's basically what Rabbit suggested in the first place :-D
Oct 16 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Gary MacKay | last post by:
I have a typical setup with a master table that has a related table with many records per master record. Since MySql does not support sub-selects, how do I delete just the set of records I want? ...
17
by: Steve Jorgensen | last post by:
Terminology question: Is there a term for a set of records related directly or indirectly by key value in several tables? For example, a single invoice record and its line item records -or- a...
0
by: Jonathan Fisher | last post by:
Whatıs the appropriate way to create records in related, subsidiary tables when a record is created in a main table? Iım using Access 2000, and I have a main table called tblProject, and a few...
2
by: Colm O'Hagan | last post by:
Hi there, I having a problem with a database I'm setting up, I would be delighted if someone out there could help. The database I'm setting up is a task register datebase, it will be used to...
7
by: amywolfie | last post by:
Is there a simple way to code: If (no related records) in VBA? I would be going from frmMain to frm2, where frm2 may or may not have a related record. I do have a PK_ID in frmMain and a...
11
misscrf
by: misscrf | last post by:
I have received data which contains similar records, where the customers have similar records. A customer, i.e. Jane Doe may have moved, or something, or a change to her account. When that happened...
5
by: upwardgazing | last post by:
I'm using Access 2003 (Access 2000 file format) and I have two tables related one-to-many called tblTempEncounter and tblTempEncounterDetails. I need to move a record from the first table with it's...
0
by: cannonpm | last post by:
Greetings and salutations. I have developed an A2K3 MDB and have a search form modeled after Allen Browne's search form (http:// allenbrowne.com/ser-62.html). It works well except for records which...
5
WyvsEyeView
by: WyvsEyeView | last post by:
Upon clicking Delete to delete the current record on frmTopics, I want several things to happen: 1) Display a custom message rather than Access's standard "You are about to delete n records" one....
10
by: Daedalus | last post by:
I'm trying to use relationships to link more than two tables. Using referencial intrgerity I can connect two tables so that by clicking a small + I can open one inside the other for the various...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.