1

I have an INNER JOIN query that runs perfectly fine by itself, which I want to LEFT JOIN to a another query/SELECT statement which contains WHERE clause. I'm not able to join both the queries. It should link wt.tkinit = t.tkinit Can you please suggest what I'm missing.

SELECT c.clnum, m.mmatter, ot.tkinit AS 'otkinit', wt.tkinit AS 'wtikint', t.tkrt01,
SUM(mt.mthrwkdb) AS 'whrs2010',
FROM client c, matter m, timekeep ot, timekeep wt, mattimhs mt, periodt p, timerate t
WHERE c.clnum = m.mclient
AND m.mmatter = mt.mtmatter
GROUP BY c.clnum, m.mmatter, ot.tkinit, wt.tkinit

SELECT t.tkinit, t.tkrt01
FROM timerate t
INNER JOIN (
SELECT tkinit, max(tkeffdate) as max_effdate 
FROM timerate WHERE DATEPART(year, tkeffdate) = '2012'
GROUP BY tkinit) mt ON mt.tkinit = t.tkinit AND mt.max_effdate = t.tkeffdate

2 Answers 2

1

Did you try the following:

SELECT fields from (SELECT1) s1 
  LEFT JOIN 
  (SELECT2) s2 
  on s1.wt.tkinit = s2.t.tkinit.

Don't forget to get normal name for wt.tkinit and t.tkinit in their selects like wt_tkinit and t_tkinit because you can't do double aliases.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry no luck. What is that s1 and s2 represent. temp table?
This is your two queries. Put it instead of SELECT1 and SELECT2
0
SELECT temp2.tkinit, temp2.tkrt01, temp.clnum, temp.mmatter, temp.otkinit, temp.tkrt01, temp.whrs2010
FROM
    ( SELECT c.clnum, m.mmatter, ot.tkinit AS 'otkinit', wt.tkinit AS 'wtikint', t.tkrt01,
    SUM(mt.mthrwkdb) AS 'whrs2010',
    FROM client c, matter m, timekeep ot, timekeep wt, mattimhs mt, periodt p, timerate t
    WHERE c.clnum = m.mclient
    AND m.mmatter = mt.mtmatter
    GROUP BY c.clnum, m.mmatter, ot.tkinit, wt.tkinit ) temp
LEFT JOIN 
    SELECT t.tkinit, t.tkrt01
    FROM timerate t
    INNER JOIN (
    SELECT tkinit, max(tkeffdate) as max_effdate 
    FROM timerate WHERE DATEPART(year, tkeffdate) = '2012'
    GROUP BY tkinit) mt ON mt.tkinit = t.tkinit AND mt.max_effdate = t.tkeffdate ) temp2
ON temp.wtikint = temp2.tkinit

Be careful of your field names, but this may be a start

2 Comments

I'm getting this erroe message
Check your field names

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.