I have a table where I have employee data by week... Let's say I just have 3 employees. I need to track their information weekly, because we might change their supervisor in any week and employee performance will also count towards supervisor performance
week
employeeId
employeeName
supervisor
2022-07-10
1
David
Bob
2022-07-10
2
Joe
Bob
2022-07-10
3
Miriam
Martin
2022-07-17
1
David
Bob
2022-07-17
2
Joe
Bob
2022-07-17
3
Miriam
Martin
I have another table, where I track sales, I just have the employeeId showing to that table.
week
employeeId
sales
2022-07-10
1
$500
2022-07-10
2
$400
2022-07-10
3
$309
I want to create a new table that show how's their supervisor with left join
FROM company.sales AS t
LEFT JOIN company.employee_roster AS roster
ON
roster.week = t.week
AND roster.employeeId = t.employeeId
But I always get duplicated data... If I remove the JOIN statement, I get the data as intended without the supervisor's name.
Is the above approach the right one? Is there any better way to join those tables?
Use below
SELECT *
FROM `company.sales` AS t
LEFT JOIN `company.employee_roster` AS roster
USING(week, employeeId)
with output
or just use below (this cover scenario when columns to be joined can have different names)
SELECT t.*, employeeName, supervisor
FROM `company.sales` AS t
LEFT JOIN `company.employee_roster` AS roster
ON t.week = roster.week
AND t.employeeId = roster.employeeId
Related
I have the below tables:
Corporate table:
CorporateId DirectorId ManagerId SalesId
1 1 1 1
2 2 2 3
3 3 4 5
Employee table:
EmployeeId FirstName LastName
1 Tim Sarah
2 Tom Paulsen
3 Tam Margo
4 Eli Lot
5 Ziva Lit
I want to display, for one corporate,the names of the Director, Manager and Sales in rows. Example with corporate 3:
EmployeeId FirstName LastName
3 Tam Margo
4 Eli Lot
5 Ziva Lit
How can I do that? I know how to display rows as columns using pivot, but unsure if pivot can be used here also.
Any help please?
You may join the two tables as the following:
SELECT E.EmployeeId, E.FirstName, E.LastName
FROM Employee E JOIN Corporate C
ON E.EmployeeID IN (C.DirectorId ,C.ManagerId ,C.SalesId)
WHERE C.CorporateId=3
See a demo.
You can first un-pivot your rows into columns by using cross apply, after which you simply join the pivoted rows to your employee table:
select e.*
from corporate c
cross apply (
select EmployeeId from (
values (Directorid), (ManagerId), (SalesId)
)r(EmployeeId)
)r
join employee e on e.EmployeeId = r.EmployeeId
where c.CorporateId = 3;
S.no
emp_id
emp_name
Dept
1
100
John
Sales
2
100
John
Accounts
3
200
Mike
Sales
4
300
Mark
Sales
5
300
Mark
Accounts
6
400
Tom
Sales
I need to pull all the emp_id who are linked ONLY to Sales Dept and ignore the ones that are in both Sales and Accounts. I am using DB2 z/os. Any suggestions would be helpful? Thanks in advance.
An anti-join will produce the result you want.
For example:
select s.*
from employee s
left join employee a on a.emp_id = s.emp_id and a.dept = 'Accounts'
where s.dept = 'Sales' and a.emp_id is null
For good performance you can try adding the index:
create index ix1 on employee (emp_id, dept);
I need to get contact information where employee id is null and not null. How do I join the same table with these different conditions. I need the information to populate a report with both employee information and person accompanied them to a event. Here is the query I have so far.
select events.id, (persons.firstname+' '+ persons.lastname) as employee
from events
inner join eventscontacts on events.id = eventcontacts.events_id
inner join contacts on eventcontacts.contact_id = contacts.id
inner join persons on contacts.person_id = person.id
Eventcontacts table
Id ContactType_id contact_id event_id
1 1 1 300
2 2 3 300
Contact type is 1 for employee and 2 for non emplopyees
contacts table
Id person_id employee_id
1 100 200
2 101 201
3 102 NULL
4 103 202
5 104 203
Person table
Id firstname lastname
100 John Stewart
101 Greg Larry
102 Kim Hans
103 Gloria June
104 Dan Duke
Result table
ID employee accompany
300 John Stewart Kim Hans
right now, I have information of all the employees for the event. I want the people who accompanied these person for the events. Their employee id is null in the contacts table. How do I join the contacts table again here?
An inner join will return only the rows that exist in both tables, where it seems like you want all the rows from the contacts table including the rows that don't match due to them lacking an employee id.
If you use an outer join, it will return rows that exist in contacts AND in events like an inner join but ALSO rows that ONLY exist in events and rows that ONLY exist in contacts.
In the case that I am explaining this poorly, I recommend you read this to help explain:
https://mode.com/sql-tutorial/sql-outer-joins/
If you can successfully use an outer join you will get all the visitors in one table regardless of having an id or not.
I want to join fields from different tables into one doing a query with joins. From now I've only joined two tables but I have difficulties merging others more. Can you help me? These are my tables:
Table Departments
------------------------------------
Department_ID Department_Name
------------------------------------
1 Sales
2 Marketing
3 Warehouse
Table Roles
---------------------------------
Role_ID
---------------------------------
1
2
3
4
Table Departments_Roles
--------------------------------------------------------------------
Dep_Role_ID Department_ID Role_ID Role_Name
--------------------------------------------------------------------
1 1 1 Admin
2 1 2 Client Attention
3 2 1 Admin
4 3 2 Client Attention
Table Employers
---------------------------------
Employer_Id Employer_Name
---------------------------------
1 John
2 Jess
3 Tom
4 George
5 David
What I want to see is:
Table Merged
-------------------------------------------------
Department_Name Employer_Name Role_Name
-------------------------------------------------
xxxxx yyyyy zzzz
This are just some example tables. Dont look for the sense of it.
I've tried using join, but I've never make something so complex.
Can you give me some advice?
Joining multiple tables is the same as joining one table, just repeated.
SELECT D.Department_Name, E.Employer_Name, DR.Role_Name
FROM Employers E --this is your base table
INNER JOIN magicalEmployerToDepartmentRoleLinkTable EDR --this connects our base table to the linking table
ON E.Employer_Id = EDR.Employer_Id
INNER JOIN Department_Roles DR --now we can pull any column in the Department_Roles table that is related back to our base table
ON ED.Department_Id = DR.Department_Id
INNER JOIN Department D --now we can pull any column in the Department table that is related back to the Department_Roles table
ON DR.Department_ID = D.Department_Id
good day, i have these 3 tables...i.e.;
customer table
cust_id cust_name sales_employee
1 abc 1
2 cde 1
3 efg 2
transaction table
order_num cust_id sales_employee
1001 1 1
1002 2 2
sales_employee table
sales_employee employee name
1 john doe
2 jane doe
how can i show the employee name on both customer table and transaction table?
notice how the sales_employee can change per transaction, it does not necessarily have to be the same per customer.
please help.
To select customers with sales person name
select
C.*, E.employee_name
from
Customers as C
inner join Sales_Employees as E on E.sales_employee = C.sales_employee
To select transactions with customer name and salesperson name (at the point in time of the transaction)
select
T.*,
E.employee_name as Trans_employee,
C.cust_name,
EC.employee_name as Cust_employee
from
Transactions as T
inner join Sales_Employees as E on E.sales_employee = T.sales_employee
inner join Customers as C on C.cust_id= T.cust_id
inner join Sales_Employees as EC on EC.sales_employee = C.sales_employee
This code is meant to guide you, you will need to adjust it to match your table and field names.