SubForm exclusions Microsoft Access - sql

I have a practice database I'm developing in order to become familiar with access. I have created 3 tables, Employees, Computers and Warranty. In the Employee Table there is an Eid (PK), First Name, Last Name and Location. In the computer table there is an Eid(FK), Serial Number (PK) and various identifiers such as brand, size,etc.
Problem
I want the form to show only the employees who currently have a device followed by the devices they have displayed in a subform. *see below
Employee
Rachel Downs
Devices
Dell Latitude
Apple iPad
Instead I get the following result
If I choose sort by employee with a subform, each employee is displayed with the devices they own. Including employees who don't have any devices. I only want to display employees who currently have devices assigned. Any suggestions on how to do this? fyi I used the form wizard to create the forms.

In the parent form invoke the query builder on the recordsource. You'll need to write a query that joins on Employees and Computers and then groups on Employee.
SELECT Employees.Eid
FROM Employees INNER JOIN Computers ON Employees.Eid = Computers.Eid
GROUP BY Employees.Eid
You can add other fields as needed from the Employees table just make sure to group on them as well.

Related

Populate SQL query with blank rows

(This is a general SQL question, but I am specifically using MSAccess 2010 so looking for how to do this with Access' flavor of SQL)
I have a table called offices which has id, office_name, num_desks.
Another table called employees which has id, employee_name.
And a final table called employee_offices which has id, office_id, employee_id.
I can assign employees to offices via employee_offices.
I am trying to generate a report which shows all offices and the employees assigned to the, but also includes blank lines for any empty desks in that office.
I realize a "simple" way to do this would be to create a desks table with id, office_id, delete the num_desks column from the offices table and change employee_offices to something like employee_desks. Then my report would be a simple LEFT OUTER JOIN and it would include all the unassigned desks. However for the sake of sanity (in this case, there is no contextual difference between desks), I am not going to do this. Plus if I start deleting desks I have referential constraints to deal with (which obviously exist for a good reason and would catch the fact that I am leaving employees without a desk), but I just want to be able to change the number of desks.
I can calculate the number of empty desks (or lack of desks) through the following command:
SELECT
office_id,
num_desks - num_employees AS desk_diff,
MAX(0, num_desks - num_employees) AS blank_rows_to_add
FROM offices LEFT OUTER JOIN (
SELECT office_id, COUNT(employee_id) AS num_employees
FROM employee_offices
GROUP BY office_Id
) AS num_employees_by_office ON offices.id = num_employees_by_office.office_id
Is there a way to take this number (blank_rows_to_add) and somehow utilze it to add that many blank rows (or at least the row only has the office_id/office_name) to a report showing a list of employees by office? I know this can be done with VBA but I am specifically looking for an SQL method that also doesn't include a temp table if at all possible.
Thank you.

How to add multiple employees to a Product

I am very NEW to SQL and know few things here and there.
I am puzzled with this for a couple of days and can't find an answer for it. What I am trying to do is to assign multiple people to a project in way that their name would appear based on their Job Assignments.
For example:
I have table called Products and in that table I have:
Model_ID -> This is set to Primary Key
Model_Name,
PM,
Designer
I have another table called Employee and in that table I have:
Employee_ID -> This is set to Primary Key
First_Name,
Last_Name,
email,
Job_Title
Under Job_Title, for each Employee, I would have their position in the company like PM, Designer, AE, Server PM, etc.
What I am trying to do is to list a corresponding Employee name under a Products table PM column if person is assigned as a PM and under Designer column if person is assigned as a Designer for that product. I have included screenshot of my diagram...
I understand I don't have them linked because everything I tried so far, I couldn't make it work. I am just trying to illustrate what I'm trying to do.
Any help/ pointers would be greatly appreciated and I am open to create new tables if needed (like separate job functions out of Employee table into a separate table, etc.)
The end result should look like:
If I'm understanding your question correctly, the following should do the trick:
Select p.Product_ID, p.Model_Name, e1.First_Name as PM, e2.First_Name as Designer
from Product p
left join Employee e1 on e1.Employee_ID = p.PM
left join Employee e2 on e2.Employee_ID = p.Designer
If you want to use full names, then:
Select p.Product_ID, p.Model_Name, e1.First_Name + ' ' + e1.Last_Name as PM, e2.First_Name + ' ' + e2.Last_Name as Dessigner
from ...
I used Left Joins in case not every product has both a PM and Designer assigned yet. You should probably have two foreign key relationships on your project table though, one for PM and one for Designer, and both will use the Employee table's Employee_ID as the key.
Design View
If you want to do this in Design View, I think you will need to add a second copy of the Employee table and 'link' the PM field to Employee_ID in one Employee table and the Designer field to Employee_ID in the other.
I hope this helps.

Relation between two join tables

I have the following tables: Department, Section, Employee,Manager, and Position. The relations between the tables are as follows:
Each Department contains many sections
Each Department contains many positions and each position might be shared with different departments
Each department section has a manager
Each manager might has different positions, for instance, Urology Department Chairman is a position as manager, and as an employee he is a Urology Consultant. As an employee, he has two positions.
As you can see from the image above
DepartmentSections is a Join table between Departments and Sections
DepartmentPositions is a Join table between Departments and Positions
My problem now, How I should configure Managers? I am thinking of making two relations, first between the Manager and DepartmentSections and between the Manager and DepartmentPositions, the problem here is that I will get two DepartmentId fields in the table Manager.
Is there any mistake in my analysis? how to build this model in order to avoid 2 DepartmentId in the table Managers?
Some thoughts on your post. It is not an answer (too long to comment) but might help a bit.
Each Department contains many sections
From this I guess that you don't need the lookup table DepartmentSections since this sentence describes one-to-many and not many-to-many relationship. You could add the DepartmentId in the Sections table.
Each department section has a manager
So you could add the ManagerId to Sections table.
Now for positions and managers, If I understood correctly, I would add the ManagerId to DepartmentPositions.

MS Access 2013 - How do I select unique values from another table?

I have a database with three tables; tbl_Room, tbl_Guest and tbl_RoomGuest (a bit simplified).
`tbl_Room` has information about a certain room
`tbl_Guest` has information about a certain guest
`tbl_RoomGuest` has information about what guest stayed at which room,
and the dates they stayed.
I am making a form where I can enter information about a room, and I want to display a list of the guests that have stayed in that room.
How can I select the names from tbl_Guest, when I want to display unique names of only the guests that stayed in that room?
I want the field to be non-editable.
The query to get the unique names could be written using distinct. Here is an example:
select distinct g.GuestName
from tbl_RoomGuest as rg inner join
tbl_Guest g
on rg.GuestId = rg.GuestId
where rg.RoomId = YOURROOMIDHERE;

sql: how to query foreign key not based upon ID

My table IncomingLetter has a foreign key to a table Department, which has an ID and a column Short_Name.
I'm using this query to count the incoming letters assigned to a department.
SELECT COUNT(DocumentNumber) AS TotalNumberIncomingLetters
FROM IncomingLetter
WHERE Assigned_To_Department=1;
Whereas this works I want to make a query based upon the short name and not based upon the ID.
SELECT COUNT(DocumentNumber) AS TotalNumberIncomingLetters
FROM IncomingLetter
WHERE Assigned_To_Department.Short_Name="My Department Name";
This does not work, whereas I found examples that are using this syntax. However, it is probably important to notice, that I m using this query in MS access.
You should use
SELECT COUNT(il.DocumentNumber) AS TotalNumberIncomingLetters
FROM IncomingLetter il
INNER JOIN Department d on d.ID = il.Assigned_To_Department
WHERE d.Short_Name="My Department Name";
The "My Department Name" text is actually stored in the Departments table, and only the number (1) is stored in the IncomingLetter table, in the field Assigned_To_Department.
Asking for Assigned_To_Department.Short_Name basically asks the number 1 to get it's Short_Name field, that does not make sense.
You need to tell the database engine two things in these scenarios:
which tables are connected - IncomingLetter and Departments in this case (the inner join part)
how they are connected - by setting their Assigned_To_Department and ID fields respecively (the on ... part