Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi guys,

I'm getting the problem in joining of multiple tables in sql server.

i have three tables(Emp, Equipment & job)...

& i'm joining them like this:

alter proc Sp_GetEmpEqp
@jobid int
as

-- Sp_GetEmpEqp 83

declare @EqpCode int

SELECT	
		j.JobID,
		eq.EquipmentCode,
		e.EmployeeCode,
	    e.EmployeeName,
		CASE WHEN dbo.GetEmployeeShift(e.EmployeeID) = 0 THEN 'Day'
			 WHEN dbo.GetEmployeeShift(e.EmployeeID) = 1 THEN 'Night'
			 END AS 'Shift'
 FROM Job j
left join Employee e on e.LocationID = j.JobID
left join Equipment eq on eq.JobID = j.JobID
where JobCategoryID = 3 and j.[Status] = 0 and j.JobID = @jobid


this query is executing multiple rows.

i have tried it with 'inner join' as well.

for eg, on job id 83 i'm getting,

83, e1, emp1
83, e2, emp1
83, e3, emp1
83, e1, emp2
83, e2, emp2
83, e3, emp2

but i'm looking for:

83, e1, emp1
83, e2, emp2
83, e3, null

can anyone plzzz help me
Posted
Updated 12-Aug-14 6:21am
v2
Comments
Herman<T>.Instance 12-Aug-14 11:12am    
e.LocationID = j.JobID ?? can this be the cause?
abdul subhan mohammed 12-Aug-14 11:19am    
No, if i remove any join of any one of the table, then it giving the correct result...
Corporal Agarn 12-Aug-14 14:58pm    
What is the schemas of the three tables?
Can table equipment be related to table employee with any other criteria?
Magic Wonder 13-Aug-14 1:34am    
Kindly share your table structures.
coded007 14-Aug-14 1:44am    
Problem is with e.LocationId = j.Jobid please verify once the Schemas of tables.

1 solution

This looks like a "cartesian product" issue: your table relationships produce multiple rows across your joins. To really solve the problem, I'd need to see the schema and possibly the data.

Is the join LocationID to JobID correct - it looks suspect to me (as per digimanaus' comment). Should it maybe be EmployeeCode?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900