views:

87

answers:

4

The query below fetches the wageTypeID based on the current active hiring info record and salary record.

  SELECT wt.wageTypeID
    FROM TimeSheet t 
  INNER JOIN Employee e ON e.employeeID = t.employeeID
  INNER JOIN Salary s ON s.salaryID = t.salaryID
  INNER JOIN Wage w ON w.wageID = s.wageID
  INNER JOIN EmpHiringInfo ehf ON ehf.EmpHiringInfoID = s.EmpHiringInfoID
  INNER JOIN WageType wt ON wt.wageTypeID = w.wageTypeID
  WHERE ehf.employeeID = 300  
    AND ehf.isActive = 1 
    AND s.isActive = 1

The above query should return the value 15! I wonder where did i go wrong?


Here is the schema of the joined tables:

TimeSheet:
timeSheetID
employeeID - 300
salaryID
.
.

Salary:
salaryID
EmpHiringInfoID
wageID
isActive - true
.
.

WageType:
wageTypeID - 15
wageTypeTitle - Hourly

Wage:
wageID
wageTypeID - 15
wageAmount - $11


EmpHiringInfo:
EmpHiringInfoID
employeeID
isActive - true
.
.

The isActive in Salary is only and only True for one record. Same thing applied to EmpHiringInfo.

+5  A: 

Try commenting out one join and see if you get any results back, if not comment out another and so on until you start seeing results. The last one you commented out might give you a clue as to why its failing.

Si Keep
All joined tables are necessary. If i comment one of them, the whole query fails
Beginner_Pal
It may not give you the answer you are looking for as you comment them out but it is just a way of debugging to try and find where the problem is.
Si Keep
I found this part causing trouble: AND s.isActive = 1 ... If i remove it i get 3 duplicate records that has value 15 ... I'm very sure Active in salary in set to true, when i add s.isActive = 1 i should get a single 15 .... The weird thing is when i put s.isActive i get no values and when i remove it i get 3 duplicates. Anyway, i will try to figure it out
Beginner_Pal
A: 

Rebuild the Select SQL a join at a time, starting with EmpHiringInfo and Salary, since those are the core ones referenced in your where clause.

Rawheiser
+2  A: 

Without an actual data dump (and the create table statements), we can't exactly say because we can't see your data. So here's how to approach the situation so you can find out where the issue is:

  1. Scale back the SELECT statement to have no JOINs--you want to make sure the base table returns the record(s) you need before going any further.
  2. Add a JOIN, one at a time. Each JOIN you add, you need to check the resultset to see that the rows you're expecting are still visible. If a JOIN has more than one criteria (IE: ehf), then again--incrementally add criteria while checking every time.

Eventually, the criteria that is causing the issue will become obvious and so you'll have to review how to handle the situation. It's possible more than one JOIN is causing troubles, and you might not see the others until after one is addressed.

I recommend restructuring your query to use the table you're selecting the most data from in the FROM clause. In this case, that means:

SELECT wt.wageTypeID
  FROM WAGETYPE wt

That will make adding JOINs easier.

OMG Ponies
I found this part causing trouble: AND s.isActive = 1
Beginner_Pal
If i remove it i get 3 duplicate records that has value 15 ... I'm very sure Active in salary in set to true, when i add s.isActive = 1 i should get a single 15.
Beginner_Pal
The weird thing is when i put s.isActive i get no values and when i remove it i get 3 duplicates. Anyway, i will try to figure it out
Beginner_Pal
@Beginner_Pal: You need to look at the `isactive` value associated with the rows returned. Doesn't look like the value is 1, so it is a matter of you don't have data to support the query--or bad data?
OMG Ponies
For some reason! The same code suddenly worked after i refreshed some of the joined tables! ....
Beginner_Pal
+2  A: 

Look at the actual execution plan. Mouse over the arrows. Look and see where the number of rows returned from a JOIN becomes zero.

Martin Smith