Postgres Script: Find Employees not in the same Location as Manager permisson - sql

Hi Im trying to get the following result below
We have three tables Employee, Permission, and Reporting(manager)
Supposed to be managers should only be assigned to employees in locations that they have permission too but this isn't what happening. The reporting table overides this and we must get the employees
Here's the result we need either of the two
Basically the result must show the employees that are assigned to managers even though the managers don't have permission to the employees locations. eg. (Employee 1 is assigned as manager of employee 21 even though employee 1 doesn't have manager permission for location 25)
I'm having a hard time with the conditions to join the three table the produce the results we need

Related

How to add x amount of employees to a stationlog?

I have an ERD.
The Stationlog table logs which employee worked at the station and at what time the employee logged in and out. The station produces orders.
As of now there can only be 1 employee logged in at a station but I am trying to figure out how I can have multiple employees in a stationlog. I want one Employee to be the manager within that Stationlog and give him the ability to add an infinite amount of employees to that stationlog.
I have tried adding fields such as employee1, employee2, employee3 in the Stationlog (not shown in this ERD) but that means I cannot add an infinite amount of employees to each stationlog.
How would I be able for one manager (who is an employee) to be able to add x amount of employees to 1 Stationlog?
Your current design is that one specific Stationlog row refers to exactly one Station and one Employee. Another Employee for the same Station would imply that there's a different Stationlog row for that other employee. (hopefully not overlapping with the first). Nothing prevents 10 employees working at the same time on the same station, each with a different log entry.
If this is not sufficient, and you want one employee define the "session" with login and logout time but allow several employees on that log, you'd need to make the relation Stationlog >o----|< Employee many-to-many.
Of couse many-to-many is easily drawn on the diagram, but would require an association table for its implementation to hold the valid pairs.
Stationlog >o------ Logteam ----|< Employee
------------
StationlogID
Employee
(OrderOfArrival etc...)
In Logteam you'd put everything which is specific go a given session and a given employee, such as arrival/departure, if this could be different from login/logout. If managers are predefined, you'd put a IsManager flag in Employee table. But if it's dynamic, e.g. the first who come (or has the key for the station), then you'd put this flag in this association table as well

How to filter a table in SQL Server

I have the following query :
WITH supervisors AS
(
--Query that returns all of the supervisors in a department
SELECT DISTINCT
sm.fk_manager AS id_supervisor,
supervisor_manager.fk_manager AS id_supervisor_manager,
s.email,
s.fk_branch,
s.fk_department
FROM global.staff_managers sm
INNER JOIN global.staff s
ON s.id_staff = sm.fk_manager
INNER JOIN global.staff_managers supervisor_manager
ON supervisor_manager.fk_staff = sm.fk_manager
WHERE s.fk_branch = 1 AND fk_department = 18
)
SELECT * FROM supervisors
-- It sometimes happens that a department has many supervisors,
--but one or many of these are the supervisors of the rest of the supervisors as well
--and these are the people we want to show, so we get rid of those supervisors whose superior
--is in the list of supervisors of that department
WHERE id_supervisor_manager NOT IN (SELECT id_supervisor FROM supervisors)
Which returns the principal supervisor/s of a department in a company
I would like to have a query which returns these supervisors without the need to indicate a department
I first tried counting the number of times a supervisor appeared as manager of supervisors for a specific branch and then selecting that supervisor who was in charge of more supervisors in his department, however it didn't work as there were supervisors who were in charge of other supervisors who also were in charge of other supervisors, thus putting a subordinate at the same level as his superior
Sample information :
Supervisors of a specific department (sample 1):
Supervisor I recieve in the current stored procedure as he is the superior of the rest of the supervisors in the department (sample 1) :
Supervisors in other department sample(2) :
Supervisor that needs to be retrieved :
The desired result :
I would really appreciate if someone helps me find a way to return the top supervisors of an specific branch
To get all principal departments' supervisors means to return all managers whose heads have another department id. Am I right?
select distinct emp.fk_staff id_supervisor, emp.fk_manager id_supervisor_manager,
dep_emp.fk_branch, dep_emp.fk_department, dep_emp.email
from global.staff_managers emp
inner join global.staff dep_emp on dep_emp.id_staff=emp.fk_staff
inner join global.staff dep_man on dep_man.id_staff=emp.fk_manager
where dep_man.fk_department != dep_emp.fk_department
and dep_emp.fk_branch = 1

List faculty members who have smaller or equal salary than their supervisor by using SELF(RECURSIVE) JOIN

Below is the table Faculty
This is the query question from my assignment which is shown below:
List faculty members who have a smaller or equal salary than their supervisor. List the Social Security number, last name and salary of the faculty and supervisor.
(Hint: use a recursive join)
This is how I approached which is shown below:
SELECT Faculty.FacSSN, Faculty.FacLastName, Faculty.FacSalary, Faculty.FacSupervisor, S.FacLastName, S.FacSalary
FROM Faculty, Faculty S
WHERE NOT Faculty.FacSSN = Faculty.FacSupervisor
AND Faculty.FacSalary <= S.FacSalary
AND NOT Faculty.FacLastName = S.FacLastName
AND Faculty.FacSupervisor IS NOT NULL;
This is the following result I'm getting which is shown below
I'm getting the result of Supervisor's salary greater than or equal to Faculty member's salary. But the problem is I'm also getting result of Faculty member's salary (who are not supervisors) being greater than or equal to other Faculty member's salary.
How do I solve this problem ?
Can someone correct my SQL code ?
Access does not support recursive joins, you will have to switch the DBMS for that. Also see Is it possible to create a recursive query in Access?
That said, this problem doesn't seem to need recursive joins, because only the direct supervisor is needed, not the whole hierarchy to the top.
Looking at your query I'm not sure what you were going for using WHERE NOT Faculty.FacSSN = Faculty.FacSupervisor (There is no one being their own supervisor), but if you change that to WHERE S.FacSSN = Faculty.FacSupervisor it should work.
A cleaned up version would look like this:
SELECT F.FacSSN, F.FacLastName, F.FacSalary, F.FacSupervisor, S.FacLastName, S.FacSalary
FROM Faculty AS F
INNER JOIN Faculty AS S ON S.FacSSN = F.FacSupervisor
WHERE F.FacSalary <= S.FacSalary

SubForm exclusions Microsoft Access

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.

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