any suggestions? sure
first of all, ditch "top 100 percent" -- that always strikes me as silly
second, you don't need to explicitly CONVERT datetime strings to datetime, but if you're gonna do it, pick the right style code (you want 120, not 102)
your correlated subquery is the right idea, but it should be applied in an ON condition as part of the join, and it should be applied to the job code table
furthermore, i think there should be a correlation of pay code date to timesheet shift date
the use of "greater than or equal to the TOP 1 ORDER BY" should simply be "equal to the MAX"
well, that was a lot of changes, so i'm not 100% sure i got everything right ...
Code:
SELECT ts.ShiftDate
, ts.idJobCode
, COUNT(ts.eeLink) AS Points
FROM dbo.tbl_ProcTimesheet ts
INNER
JOIN dbo.GratBanqPointsByDate as jc
ON ts.idJobCode = jc.idJobCode
AND jc.EffectiveDate =
( SELECT max(EffectiveDate)
FROM dbo.GratBanqPointsByDate
WHERE idJobCode = ts.idJobCode
AND EffectiveDate <= ts.ShiftDate )
WHERE ts.Sequence = 0
AND ts.ShiftDate
BETWEEN '2005-11-27' AND '2005-12-07'
GROUP
BY ts.ShiftDate
, ts.idJobCode
ORDER
BY ts.ShiftDate
, ts.idJobCode