I have two tables say Educational_Info and Employee_Info. Details of these tables are given below.
Table: Educational_Info
DegreeID DegreeName
1 BBA
2 BCA
3 MBA
4 MCA
Table: Employee_Info
EmpID BachelorDegree MasterDegree
1001 2 4
Desired Output:
EmpID BachelorDegree MasterDegree
1001 BCA MCA
How to get this desired output. If not possible with this table structure, please suggest me any other way.
Thanks in advance.
This this. This gives you some idea
select b.EmpID,a.DegreeName
FROM Educational_Info a
JOIN Employee_Info b on b.BachelorDegree = a.DegreeID or b.MasterDegree = a.DegreeID
SELECT E.Empid,EI1.DegreeName,EI2.DegreeName
FROM Employee_info E
INNER JOIN Educational_Info EI1 ON E.BachelorDegree=EI1.Degreeid
INNER JOIN Educational_Info EI2 ON E.MasterDegree=EI2.Degreeid
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;
I've got 3 tables
Plan:
plan_id emp_id duration
123 1010 30
456 1011 40
789 1012 60
PlanEmp:
plan_id emp_id
123 2131
456 3131
789 4131
Emp:
emp_id Name
1010 Andy
1011 Cole
1012 John
2131 Sam
3131 Kim
4131 Ray
Desired Summary Output:
plan_id Name duration
123 Andy 30
123 Sam 30
456 Cole 40
456 Kim 40
789 John 60
789 Ray 60
Query that I'm trying to modify to get the above result:
SELECT P.plan_id
,E.Name
,P.duration
FROM Plan P
LEFT JOIN Emp E
ON P.emp_id = E.emp_id
LEFT JOIN PlanEmp PE
ON P.plan_id = PE.plan_id
I'm unable to figure how to pull the Employee details using the PlanEmp table AND the Plan table to get the summary output.
This should work:
SELECT sub.plan_id, sub.emp_id, Emp.name, Plan.duration
FROM
(SELECT plan_id, emp_id
FROM Plan
UNION
SELECT plan_id, emp_id
FROM PlanEmp) sub
LEFT JOIN Emp
ON sub.emp_id = Emp.emp_id
LEFT JOIN Plan
ON sub.plan_id = Plan.plan_id
ORDER BY plan_id
Tested here: http://sqlfiddle.com/#!9/21ca79/4
Use the below script.
;With cte_1
As( select plan_id,emp_id
From plan
UNION
Select plan_id,emp_id
From plan_emp)
Select c.plan_id,e.Name,p.Duration
From cte_1 c
Join plan p on c.plan_id=p.plan_id
Join emp e on c. Emp_id=e.emp_id
I'm sure there's a logical explanation for this and since my reputation is not at the level to comment yet, but why is emp_id on both the Plan and PlanEmp tables? If you can normalize this, that would be a good idea to move emp_id off of the Plan table to the PlanEmp table.
Your table design is not following the needed Normalization practices. You should merge the table Plan and EmpPlan.
or
Your design should be (To normalize more):
Table: Plan
Plan_ID
Duration
Table: PlanEmp
Plan_ID
Emp_ID
Your existing structure can be queried with multiple ways but they may not efficient. Another way:
Select ISNULL(P.plan_id, PE.plan_id) Plan_ID, E.Name,
(Select Duration from #Plan pp Where pp.plan_id = ISNULL(P.plan_id, PE.plan_id)) as Duration
from Emp E
left Join #lan P on E.emp_id = P.emp_id
left Join PlanEmp PE on E.emp_id = PE.emp_id
Where P.emp_id IS NOT NULL or PE.emp_id is not null
I have 2 tables respectively,emp_data, role_data
EMP_DATA
ID EMPID EMPNAME ROLEID
1 A01 ABC 1
2 A01 ABC 3
ROLE_DATA
ROLEID ROLENAME EMPID
1 SE A01
2 SSE B01
When I join these 2 tables, i have to get OUTPUT OF 2 records like the below one
EMPID EMPNAME ROLEID ROLENAME
A01 ABC 1 SE
A01 ABC 3 <NULL OR EMPTY>
The query what i have written will give output where instead of null in rolename it gives me SE.
SELECT ED.EMPID,ED.EMPNAME,ED.ROLEID,RD.ROLENAME
FROM SYN.EMP_DATA ED,SYN.ROLE_DATA RD
WHERE ED.EMPID=RD.EMPID
Kindly help me in this regard as to how to get the output like i desire.
Thanks in advance
You want a left join:
select e.EMPID, e.EMPNAME, e.ROLEID, r.ROLENAME
from EMP_DATA e left join
ROLE_DATA r
on e.ROLEID = r.ROLEID;
By the way, your problem suggests an issue with your database. EMP_DATA.ROLEID should be declared as a foreign key referencing ROLE_DATA. If so, it would be an error to insert a value that is invalid.
Show us your code. You have to use LEFT JOIN
SELECT emp.EMPID
,emp.EMPNAME
,emp.ROLEID
,ROLE.ROLENAME
FROM EMP_DATA as emp
LEFT JOIN ROLE_DATA as ROLE ON emp.ROLEID = ROLE.ROLEID;
Try this query
select a.EMPID, a.EMPNAME, a.ROLEID,IFNULL(b.ROLENAME, 'SE') as ROLENAME
from EMP_DATA a
lef join ROLE_DATA b on a.ROLEID=B.ROLEID
hi i have problem in sql query example
Employee
empid empname
1 gan
2 sam
Designation
id desig empid
1 sr officerr 1
2 jr officer 1
3 manager 2
i want join tables and want Employee Table repeated records Null
result like
empid name desig id
1 gan sr officerr 1
1 NULL jr officer 2
2 sam manager 3
i working on query but i not getting result
SELECT DISTINCT designatin.empid, employee.empname,designatin.desig
FROM designatin INNER JOIN employee e ON employee.empid = designatin.empid
GROUP BY employee.empid, employee.empname, designatin.desig
can anybody have solution?
Change the inner join to a left join:
SELECT DISTINCT designatin.empid, employee.empname,designatin.desig
FROM designatin LEFT JOIN employee e ON employee.empid = designatin.empid
GROUP BY employee.empid, employee.empname, designatin.desig
Let try this, it will help you
SELECT e.empid, e.empname,d.desig ,d.id
FROM employee e
INNER JOIN Designation d ON e.empid = d.empid
See DEMO
I have two tables. Employee and Qualifications.
Employee
Emp_id Name Qualification1 Qualification2 Qualification3
10001 xxxxxx 1 3 5
10002 yyyyyy 3 2
.......
......
.....
Qualifications
Qual_ID Qual_name
1 B.Tech
2 MCA
3 M.Tech
How can i use join query to get the following output
Emp_ID Name Qual1 Qual2 Qual3
10001 xxxxxxx B.Tech MCA pppp
10002 yyyyyyy B.Tech
......
.....
.....
try this
Select E.Emp_ID,E.Name,Q1.Qual_Name,Q2.Qual_Name, Q3.Qual_Name
From
Employees AS E
INNER JOIN Qualifications As Q1 ON E.Qualification1=Q1.Qual_ID
INNER JOIN Qualifications As Q2 ON E.Qualification2=Q2.Qual_ID
INNER JOIN Qualifications As Q3 ON E.Qualification3=Q3.Qual_ID
SELECT e.emp_id,
e.name,
q1.qual_name,
q2.qual_name,
q3.qual_name,
FROM employee e
INNER JOIN qualifications q1
ON e.qualification1 = q1.qual_id
INNER JOIN qualifications q2
ON e.qualification2 = q2.qual_id
INNER JOIN qualifications q3
ON e.qualification3 = q2.qual_id
Try this:
Sql Fiddle
Select
Emp_id, Name,
(Select Qual_name from Qualifications where Qualifications.Qual_ID = Employee.Qualification1) as Qual1,
(Select Qual_name from Qualifications where Qualifications.Qual_ID = Employee.Qualification2) as Qual2,
(Select Qual_name from Qualifications where Qualifications.Qual_ID = Employee.Qualification3) as Qual3
From Employee